How to make a list from a list of tuples. I need from [(1, 2), (3, 3)] get [1, 2, 3, 3]

Question:

I have this code

from typing import List, Tuple
n = 2
a = [1,3]
b = [2,3]
def zipper(a: List[int], b: List[int]) -> List[int]:
    zippy = []
    for i in range(len(a)):
        zippy.append((a[i], b[i]))
    return zippy
print(zipper(a, b))

How can I create a regular list from this?

Asked By: Denika

||

Answers:

Just keep it simple:

for i in range(len(a)):
    zippy.append(a[i])
    zippy.append(b[i])

You do know there’s zip in python, right?

Answered By: gog

Sorry, just realised from the title of the post that you want [1, 2, 3, 3] from [(1, 2), (3, 3)]:


list(itertools.chain.from_iterable(zipper(a, b)))

>> [1, 2, 3, 3]
Answered By: Azhar Khan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.