0

I have a list m:

m = ['Apple', 'Lemon', 'Orange']

and I want output as follows:

['Apple'], ['Lemon'], ['Orange']

How can this be done without using list comprehension and for cycles?

In my approach I dont understand what variables to use. Any ideas?

def lists(m):    
    count=0`
    while count < len(m):            
        len(m)
        count += 1
        
    return #separated lists?

print(lists(m))
4
  • Are you wanting the problem solved without using list comprehension with for-loops or without using either list comprehension or for-loops? Commented Dec 28, 2020 at 23:37
  • (and are you fine with generator comprehensions?) Commented Dec 28, 2020 at 23:38
  • 1
    @l3viathan I doubt it, the OP seems to be going for a traditional while loop approach Commented Dec 28, 2020 at 23:39
  • 3
    When having such an oddly specific request as "no list comprehensions", it would be best if you could explain why exactly you are requesting that, so that the answer can account for whatever your specific reasoning is. Commented Dec 29, 2020 at 0:07

4 Answers 4

1

Since you have asked to avoid List Comprehension, you could try something like this: (BTW, while is just kind of for loop!)

m = ['Apple', 'Lemon', 'Orange']

from typing import List

def split_list(lst: List[str]):
    
    res = []
    
    for item  in lst:
        res.append([item])

    return res


print(split_list(m))
Sign up to request clarification or add additional context in comments.

2 Comments

It's a little strange that you create a count variable that you don't use. It's also a little weird to use enumerate if you don't plan on using the index. Why not for item in lst:? On the other hand the OP was asking not to use for loops.
You're absolutely right. My bad - left from the OP 's code. Correct it now.
0

Here's a traditional while loop approach. Details of how it works are in the comments.

m = ['Apple', 'Lemon', 'Orange']

def list_to_lists(lst):
    i = 0

    # this will be the result list, containing lists of strings
    res = []

    while i < len(lst):

        # [lst[i]] creates a new list containing the i'th element of lst
        # res.append will append this to the result list
        res.append([lst[i]])

        i += 1

    # return the result list
    return res

lists = list_to_lists(m)
print(lists)

Its been an incredibly long time since I've actually written a while loop. Do note that for loops are often neater for this sort of thing - see @daniel-hao's example.

Comments

0

No list comprehension, presumably no "for cycles" (might depend on what you mean), and results in a tuple as requested.

>>> *map(lambda s: [s], m),
(['Apple'], ['Lemon'], ['Orange'])

Comments

0

you could use recursion and parameter unpacking

def makeLists(first,*rest): 
    return [[first]] + (makeLists(*rest) if rest else [])

m = ['Apple', 'Lemon', 'Orange']

print(makeLists(*m))

# [['Apple'], ['Lemon'], ['Orange']]

You can also make it without parameter unpacking:

def makeLists(strings): 
    return ([strings[:1]] + makeLists(strings[1:])) if strings else []

m = ['Apple', 'Lemon', 'Orange']

print(makeLists(m))

# [['Apple'], ['Lemon'], ['Orange']]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.