Bottom Page

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.
Quote
#2
(Jan-31-2018, 04:47 PM)AyCaramba Wrote: Does the list(zipped) command in line 4 somehow destroy my zip object?
The zip object is an iterable. The expression list(zipped) consumes the iterable. There are no more items in zipped after this expression.
buran likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  why my function doesn't work cimerio 3 109 Nov-20-2019, 01:13 PM
Last Post: ChislaineWijdeven
  For loop in my __init__ doesn't work as expected Jessy 2 94 Nov-18-2019, 10:07 AM
Last Post: buran
  Type function does not work sunnyarora 2 357 Mar-15-2019, 10:50 AM
Last Post: sunnyarora
  Rewrite a function to make it work with 'bottle-pymysql' nikos 1 281 Feb-26-2019, 02:59 PM
Last Post: nikos
  Why this simple function doesnt work? blackknite 8 555 Jan-05-2019, 12:32 PM
Last Post: buran
  Function not returning expected value Euqinu 4 728 Sep-10-2018, 12:48 PM
Last Post: Euqinu
  len() function doesn't work with Geany Editor hudabaig 2 930 Jun-01-2018, 11:20 PM
Last Post: wavic
  Why won't this user created function work? Evyeniarocks 5 875 Mar-29-2018, 05:20 PM
Last Post: wavic
  Recursive function not returning expected output...(Python speech recog module) bigmit37 4 2,226 Jan-10-2017, 02:13 PM
Last Post: bigmit37
  Any way to get raw_input to work in my function? Pythonerous 4 2,023 Oct-27-2016, 05:03 PM
Last Post: Pythonerous

Forum Jump:


Users browsing this thread: 1 Guest(s)