Skip to content Skip to sidebar Skip to footer

Merge Different Lists Together In Python

I am so called newbie in Python. I have difficulties with lists. I have a loop which take some info from textfile and goes through function. If textfiles lenght is 10 rows then out

Solution 1:

If you have all of your lists stored in a list a,

# a = [[.45], [.87], ...]import itertools
output = list(itertools.chain(*a))

What makes this answer better than the others is that it neatly joins an arbitrary number of lists together in one line, without a need for a for loop or anything like that.

Solution 2:

You might want to have a look at the following questions:

Basically, if you're reading your lines in a loop, you can do like

result= []
for line in file:
    newlist = some_function(line) # newlist contains the result list for the current line
    result=result+ newlist

Solution 3:

You can just add them: [1] + [2] = [1, 2].

Post a Comment for "Merge Different Lists Together In Python"