Python Forum
zip() function does not work as expected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
zip() function does not work as expected
#1
This is my code for creating 2 lists, zipping them together, unzipping them and printout the unzipped lists.

list1 = [1, 2, 3]  # create 2 lists: list1, list2
list2 = ['A', 'B', 'C']
zipped = zip(list1, list2)  # create zip object from the 2 lists: zipped
test1, test2 = zip(*zipped)  # unzip the zip: test1, test2
print(list(test1))
print(list(test2))
As expected, I get the output:
Output:
[1, 2, 3] ['A', 'B', 'C']
Now, I want to print the values of zipped, using list(zipped) after defining my zip object. After line 3, I insert the additional print command.

list1 = [1, 2, 3]  # create 2 lists
list2 = ['A', 'B', 'C']
zipped = zip(list1, list2)  # create zip object from the 2 lists: zipped
print(list(zipped))
test1, test2 = zip(*zipped)  # unzip the zip using *
print(list(test1))
print(list(test2))
I get an error massage:
Error:
line 5, in <module> test1, test2 = zip(*zipped) # unzip the zip using * ValueError: not enough values to unpack (expected 2, got 0)
Why do I get an error? Does the list(zipped) command in line 4 somehow destroy my zip object?
When I recreate zipped in line 5, after printing its value out, everything works fine again.

list1 = [1, 2, 3]  # create 2 lists
list2 = ['A', 'B', 'C']
zipped = zip(list1, list2)  # create zip object from the 2 lists: zipped
print(list(zipped)) 
zipped = zip(list1, list2)  # recreate zip object from the 2 lists: zipped
test1, test2 = zip(*zipped)  # unzip the zip using *
print(list(test1))
print(list(test2))
That's now the output I expected in the second code.
Output:
[(1, 'A'), (2, 'B'), (3, 'C')] [1, 2, 3] ['A', 'B', 'C']
I dont understand why I need to recreate my zip object to get the output I expected.
Reply


Messages In This Thread
zip() function does not work as expected - by AyCaramba - Jan-31-2018, 04:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  print doesnt work in a function ony 2 307 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  I dont know why my function won't work? MehHz2526 3 1,198 Nov-28-2022, 09:32 PM
Last Post: deanhystad
  time function does not work tester_V 4 3,049 Oct-17-2021, 05:48 PM
Last Post: tester_V
  write new function or change the old one to work "smartter? korenron 3 1,984 Aug-09-2021, 10:36 AM
Last Post: jamesaarr
  string function doesn't work in script ClockPillow 3 2,420 Jul-13-2021, 02:47 PM
Last Post: deanhystad
  Why does unpickling only work ouside of a function? pjfarley3 5 3,453 Dec-24-2020, 08:31 AM
Last Post: pjfarley3
  Please help my while loop does not work as expected KingKhan248 6 2,634 Sep-28-2020, 09:12 PM
Last Post: deanhystad
  Recursive function returns None, when True is expected akar 0 3,397 Sep-07-2020, 07:58 PM
Last Post: akar
  len() function, numbers doesn't work with Geany Editor Penguin827 3 3,012 May-08-2020, 04:08 AM
Last Post: buran
  Powerset function alternative does not work oClaerbout 1 2,000 Feb-11-2020, 11:34 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020