0

So this is my code. I was trying out the zip function and I wanted to print out the tuple but I end up getting nothing.

list_one = [1,2,3]
list_two = [4,5,6]
new_list = zip(list_one, list_two)
for i, k in new_list:
    print(i, k, end = ' ')
print()
print(tuple(new_list))

This is the output.

1 4 2 5 3 6 
()

why does it just return () and not the tuple with tuples inside? That is what I would expect. Thank you in advance.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
ewe
  • 159
  • 1
  • 7
  • 4
    what is list_one ?? – Ashish Karn Jul 21 '20 at 05:50
  • 1
    See also: https://stackoverflow.com/questions/17777219/zip-variable-empty-after-first-use – Niko Föhr Jul 21 '20 at 05:52
  • 1
    `list_one` was eaten by the Markdown formatting; I have fixed it for OP. For reference: if you use triple-backticks to format code, you can't put the first line of code on the same line as the opening backticks - text there is used to tell the Markdown processor what language the code is in, for syntax highlighting. – Karl Knechtel Jul 21 '20 at 05:52
  • This, Will Solve Your Problem, ```list_one = [1,2,3] list_two = [4,5,6] new_list = zip(list_one, list_two) print(tuple(new_list))``` – Aryan Jul 21 '20 at 06:00
  • `zip()` creates iterator which can be used only once. You have to convert to `list()` to use it many times. `new_list = list(zip(list_one, list_two))` – furas Jul 21 '20 at 07:14

0 Answers0