How do I convert multiple lists inside a list using Python?

Question:

I want to convert multiple lists inside a list? I am doing it with a loop, but each sub list item doesn’t get a comma between it.

myList = [['a','b','c','d'],['a','b','c','d']]
myString = ''
for x in myList:
    myString += ",".join(x)
print myString

ouput:

a,b,c,da,b,c,d

desired output:

a,b,c,d,a,b,c,d
Asked By: user2242044

||

Answers:

This can be done using a list comprehension where you will “flatten” your list of lists in to a single list, and then use the “join” method to make your list a string. The ‘,’ portion indicates to separate each part by a comma.

','.join([item for sub_list in myList for item in sub_list])

Note: Please look at my analysis below for what was tested to be the fastest solution on others proposed here

Demo:

myList = [['a','b','c','d'],['a','b','c','d']]
result = ','.join([item for sub_list in myList for item in sub_list])

output of result -> a,b,c,d,a,b,c,d

However, to further explode this in to parts to explain how this works, we can see the following example:

# create a new list called my_new_list
my_new_list = []
# Next we want to iterate over the outer list
for sub_list in myList:
    # Now go over each item of the sublist
    for item in sub_list:
        # append it to our new list
        my_new_list.append(item)

So at this point, outputting my_new_list will yield:

['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']

So, now all we have to do with this is make it a string. This is where the ','.join() comes in to play. We simply make this call:

myString = ','.join(my_new_list)

Outputting that will give us:

a,b,c,d,a,b,c,d

Further Analysis

So, looking at this further, it really piqued my interest. I suspect that in fact the other solutions are possibly faster. Therefore, why not test it!

I took each of the solutions proposed, and ran a timer against them with a much bigger sample set to see what would happen. Running the code yielded the following results in increasing order:

  1. map: 3.8023074030061252
  2. chain: 7.675725881999824
  3. comprehension: 8.73164687899407

So, the clear winner here is in fact the map implementation. If anyone is interested, here is the code used to time the results:

from timeit import Timer


def comprehension(l):
    return ','.join([i for sub_list in l for i in sub_list])


def chain(l):
    from itertools import chain
    return ','.join(chain.from_iterable(l))


def a_map(l):
    return ','.join(map(','.join, l))

myList = [[str(i) for i in range(10)] for j in range(10)]

print(Timer(lambda: comprehension(myList)).timeit())
print(Timer(lambda: chain(myList)).timeit())
print(Timer(lambda: a_map(myList)).timeit())
Answered By: idjaw
from itertools import chain

myList = [['a','b','c','d'],['a','b','c','d']]

print(','.join(chain.from_iterable(myList)))

a,b,c,d,a,b,c,d
Answered By: LetzerWille
myList = [['a','b','c','d'],[a','b','c','d']]
smyList = myList[0] + myList[1]
str1 = ','.join(str(x) for x in smyList)
print str1

output

a,b,c,d,a,b,c,d
Answered By: Alireza0119

You could also just join at both levels:

>>> ','.join(map(','.join, myList))
'a,b,c,d,a,b,c,d'

It’s shorter and significantly faster than the other solutions:

>>> myList = [['a'] * 1000] * 1000
>>> from timeit import timeit

>>> timeit(lambda: ','.join(map(','.join, myList)), number=10)
0.18380278121490046

>>> from itertools import chain
>>> timeit(lambda: ','.join(chain.from_iterable(myList)), number=10)
0.6535200733309843

>>> timeit(lambda: ','.join([item for sub_list in myList for item in sub_list]), number=10)
1.0301431917067738

I also tried [['a'] * 10] * 10, [['a'] * 10] * 100000 and [['a'] * 100000] * 10 and it was always the same picture.

Answered By: Stefan Pochmann
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.