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

Time:10-16

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?

CodePudding user response:

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]

CodePudding user response:

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?

  • Related