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?
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?
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]
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?
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?
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]