11

I want to convert list into list of list. Example:

my_list = ['banana', 'mango', 'apple']

I want:

my_list = [['banana'], ['mango'], ['apple']]

I tried:

list(list(my_list))
miradulo
  • 28,857
  • 6
  • 80
  • 93
Sameer
  • 289
  • 2
  • 4
  • 9
  • This question may be duplicated to this one: https://stackoverflow.com/questions/13109325/converting-list-to-list-of-list – ah bon Jan 23 '22 at 16:02
  • Does this answer your question? [Converting list to list of list](https://stackoverflow.com/questions/13109325/converting-list-to-list-of-list) – ah bon Jan 23 '22 at 16:02

3 Answers3

31

Use list comprehension

[[i] for i in lst]

It iterates over each item in the list and put that item into a new list.

Example:

>>> lst = ['banana', 'mango', 'apple']
>>> [[i] for i in lst]
[['banana'], ['mango'], ['apple']]

If you apply list func on each item, it would turn each item which is in string format to a list of strings.

>>> [list(i) for i in lst]
[['b', 'a', 'n', 'a', 'n', 'a'], ['m', 'a', 'n', 'g', 'o'], ['a', 'p', 'p', 'l', 'e']]
>>> 
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • What about map? I like this `map(list, lst)` – smac89 Jul 27 '16 at 06:03
  • Lol can you elaborate more on that? – smac89 Jul 27 '16 at 06:05
  • @khredos check blwo answer for map – Rakesh Kumar Jul 27 '16 at 06:06
  • 2
    @khredos `map` will work like `[list(i) for i in lst]` – Rahul K P Jul 27 '16 at 06:06
  • @RahulKP you're right, but what I proposed is less verbose. I just want to konw what makes it a bad idea. I feel like I might already know and it has something to do with representation of map in python 2 vs python 3 – smac89 Jul 27 '16 at 06:08
  • `map` applies the func which was given as first arg to each item in the iterable.. so `map(list, iterable) == [list(i) for i in iterable]` (in python2) And note that `list('foo') != ['foo']` – Avinash Raj Jul 27 '16 at 06:09
  • @khredos: Possibly because it is frowned upon when cleaner pythonic alternatives such as list comprehensions exist. Although in this particular case it looks very elegant -- I'd probably favor map. – UltraInstinct Jul 27 '16 at 06:13
  • @Sameer: Please consider accepting the answer if it solved your problem. – Cleb Jul 27 '18 at 23:15
6

Try This one Liner:-

map(lambda x:[x], my_list)

Result

In [1]: map(lambda x:[x], my_list)
Out[1]: [['banana'], ['mango'], ['apple']]
Rahul K P
  • 15,740
  • 4
  • 35
  • 52
Rakesh Kumar
  • 4,319
  • 2
  • 17
  • 30
  • 1
    Although not pythonic, this is a correct answer. Not sure why this was downvoted. Could be be made more concise: `map(list, my_list)`. – UltraInstinct Jul 27 '16 at 06:09
  • @SuperSaiyan map is little bit slower than list comp. – Avinash Raj Jul 27 '16 at 06:15
  • 1
    @AvinashRaj I prefer readability and elegance over micro optimizations of that nature. If those nanoseconds were worth a lot, I probably choose other languages. – UltraInstinct Jul 27 '16 at 06:16
  • @UltraInstinct, `map(list, my_list)` does _not_ work. `list(1)` will give a `TypeError` – aydow Mar 25 '19 at 05:21
  • @aydow how you defined your list `my_list` – Rakesh Kumar Mar 25 '19 at 07:04
  • sorry, that should be it throws an error on a list of `int`s – aydow Mar 25 '19 at 08:23
  • @UltraInstinct and @aydow `list(1)` and `map(list, my_list)` wouldn't work, `list` takes `iterable` as input but in `map(list, my_list)` my_list have values as `scaler` but not `iterable`. Check help(list). so better way is i answered. – Rakesh Kumar Mar 26 '19 at 08:23
  • You are right, `map(list, my_list)` would not work. Not sure what I was thinking. I stand by first statement though, list comprehension is more pythonic for this use case. – UltraInstinct Mar 26 '19 at 23:08
-4
    lst = ['banana', 'mango', 'apple']
    lst_of_lst = []
    for i in lst:
        spl = i.split(',')
        lst_of_lst.append(spl)
    print(lst_of_lst)

first create an empty list and then iterate through your list. split each list and append them to your empty list.