0

How can I convert

([1,2,3],[4,5]) 

to

(1,2,3,4,5) 

in Python?

I just want to convert the lists in the tuple to a single tuple, it means to remove the list.

Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58

1 Answers1

1
tpl = ([1,2,3],[4,5])

print(tuple(sum(tpl, [])))

Prints:

(1, 2, 3, 4, 5)

OR:

Using itertools.chain:

from itertools import chain

print(tuple(chain.from_iterable(tpl)))

OR:

Using comprehensions:

print(tuple(i for lst in tpl for i in lst))
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91