Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 975
user2242044 Avatar asked Dec 05 '22 20:12

user2242044


2 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())
like image 124
idjaw Avatar answered May 15 '23 15:05

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
like image 41
LetzerWille Avatar answered May 15 '23 16:05

LetzerWille