0

[(1, 2), (3, 4)] how can I add element of 2nd tuple of list into 1st tuple ? I want the final output as : [(1,2,3,4)]

3 Answers3

1

Just try itertools

import itertools
out = [tuple(itertools.chain.from_iterable(ls))]

or else, If you want the lambda function please try below:

flatten = lambda l: [tuple([item for sublist in l for item in sublist])]
flatten(ls)

Basically, you cannot edit or add the elements, but you can create a tuple by iterating the values inside tuples.

0

In the simple case, that's the simple solution:

l = [(1, 2), (3, 4)] 
res = [l[0] + l[1]]

result:

[(1, 2, 3, 4)]
Roy2012
  • 11,755
  • 2
  • 22
  • 35
0

first iteration for and inner for for elements.

x = [(1, 2), (3, 4)] 
y=[]
for i in x:
    for e in i:
        y.append(e)
print(y)