2

I already know that if we have a list of two tuples like:

list = (('2', '23', '29', '26'), ('36', '0'))

by the below command:

new_list = list[0] + list[1]

it would be;

list = ('2', '23', '29', '26', '36', '0')

What shall I do if we have a plenty of tuples the below, and I want to use something like loop command?

list = [[list], [list2], [list3], ...]

I want:

new_list = [list1, list2, list3,...]
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
mar
  • 323
  • 2
  • 8
  • 19

6 Answers6

3

Use itertools.chain, and you can simply supply the list as arguments using * to expand them.

>>> from itertools import chain
>>> a_list = [[1], [2], [3]]
>>> list(chain(*a_list))
[1, 2, 3]
>>> tuple(chain(*a_list))
(1, 2, 3)

Also do not use pre-defined types such as list as a variable name as this redefines them to not being what they really are, and the parentheses (1, 2...) syntax results in a tuple, not a list.

metatoaster
  • 17,419
  • 5
  • 55
  • 66
1
>>> main_list = [[1,2,3],[4,5,6,7],[8,9]]
>>> [item for sublist in main_list for item in sublist]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

This uses a nested list comprehension approach. A good explanation of how to read them can be found here.

Think how you'd do it with regular loops. One outer loop will extract a list and an inner loop will append every element of the list to the result.

>>> newlist = []
>>> for sublist in main_list:
        for item in sublist:
            newlist.append(item)

>>> newlist
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Similarly, in the nested list comprehension above - the for sublist in main_list extracts a sublist, the for item in sublist loops over each item and the item at the beginning of the comprehension does an automatic list.append(item) to the final result. The biggest difference from regular loops is that what you want to get automatically appended to the final result is placed at the beginning.

Maksim Kneller
  • 297
  • 1
  • 15
1

Firstly, you are not merging two lists as you say in the question. What you're doing is making a list of list into a list.

There are many ways you can do this. Apart from the ways listed in other answers, one possible solution could be:

for i in range(0, len(list_of_list)):
    item = list_of_list[i]
    for j in range(0,len(item)):
        new_list = new_list + [item]

Note: This solution is typically labelled as C - like as it doesn't make use of any Python methods.

Pranav Totla
  • 2,182
  • 2
  • 20
  • 28
0

Using sum() ,

>>> tp = ( ('2', '23', '29', '26'), ('36', '0'), ('4', '2') )
>>> newtp = sum(tp, () )
>>> newtp
('2', '23', '29', '26', '36', '0', '4', '2')

or itertools ,

>>> from itertools import chain
>>> tp = ( ('2', '23', '29', '26'), ('36', '0'), ('4', '2') )
>>> newtp = tuple( chain(*tp) )
>>> newtp
('2', '23', '29', '26', '36', '0', '4', '2')

or comprehension,

>>> tp = ( ('2', '23', '29', '26'), ('36', '0'), ('4', '2') )
>>> newtp = tuple(i for subtp in tp for i in subtp)
>>> newtp
('2', '23', '29', '26', '36', '0', '4', '2')
raymelfrancisco
  • 828
  • 2
  • 11
  • 21
0

One simple way is using reduce inbuilt method.

>>> list_vals = (('2', '23', '29', '26'), ('36', '0'))
>>> reduce(lambda x, y: x + y, list_vals)
('2', '23', '29', '26', '36', '0')
gsb-eng
  • 1,211
  • 1
  • 9
  • 16
0

In this case, all the entries of the list are integers and so it would be easy to use regular expressions. The extra advantage of using regular expressions here is that it will work on any arbitrary nested list vs. chain that does not work when the list is more than 1 degree nested.

import re
alist = [[1], [2],[3]]
results = [int(i) for i in re.findall('\d+', (str(alist)))]
print(results)

The output is;

>>> [1,2,4]

So if we are given an ugly arbitrarily nested list like:

a_list = [[1], [2], [3], [1,2,3[2,4,4], [0]], [8,3]]

we can do;

a_list = [[1], [2], [3], [1,2,3, [2,4,4], [0]], [8,3]]
results = [int(i) for i in re.findall('\d+', (str(a_list)))]
print(results)

and the output is;

>>> [1, 2, 3, 1, 2, 3, 2, 4, 4, 0, 8, 3]

Which is arguably more helpful.

Samuel Nde
  • 2,565
  • 2
  • 23
  • 23