Following on my previous question How to group list items into tuple?
If I have a list of tuples, for example
a = [(1,3),(5,4)] How can I unpack the tuples and reformat it into one single list
b = [1,3,5,4] I think this also has to do with the iter function, but I really don't know how to do this. Please enlighten me.
📢 TLDR: Use + In almost all simple situations, using list1 + list2 is the way you want to concatenate lists. The edge cases below are better in some situations, but + is generally the best choice. All options covered work in Python 2.3, Python 2.7, and all versions of Python 31.
Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
b = [i for sub in a for i in sub] That will do the trick.
In [11]: list(itertools.chain(*a)) Out[11]: [1, 3, 5, 4] If you just need to iterate over 1, 3, 5, 4, you can get rid of the list() call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With