I'm messing around with zipping 3 lists together and then unpacking them in Python to better understand lists and zip()
. In the code below, if I don't comment out print(list(zip_list))
, the unzip1-3 variables produce empty lists. If i do comment that part out, i get the lists I would expect. Why would this be?
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
list3 = ['!', '@', '#']
zip_list = zip(list1, list2, list3)
print(list(zip_list))
unzip1 = []
unzip2 = []
unzip3 = []
for i, j, k in zip_list:
unzip1.append(i)
unzip2.append(j)
unzip3.append(k)
print(unzip1)
print(unzip2)
print(unzip3)
Output:
[(1, 'a', '!'), (2, 'b', '@'), (3, 'c', '#')]
[]
[]
[]
Output, commented out:
[1, 2, 3]
['a', 'b', 'c']
['!', '@', '#']