Search code examples
pythonlisttupleslist-comprehension

How to collect list of tuples into one tuple in Python?


I think this should be a no brainer, but I'm kinda lost. If I have a list of tuples:

l = [(1, 2), (3, 4), (5, 6)]

how do I put all the values from the tuples into one list so that the result is this:

[1, 2, 3, 4, 5, 6]

I guess I need to use list comprehensions, but I'm unsure how.. All tips are welcome!


Solution

  • import itertools
    
    l = [(1, 2), (3, 4), (5, 6)]
    
    print list(itertools.chain(*l))
    

    or

    print list(itertools.chain.from_iterable(l))
    
    
    
    #output =[1, 2, 3, 4, 5, 6]