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
  len() function, numbers doesn't work with Geany Editor Penguin827 3 263 May-08-2020, 04:08 AM
Last Post: buran
  Powerset function alternative does not work oClaerbout 1 278 Feb-11-2020, 11:34 AM
Last Post: Larz60+
  why my function doesn't work cimerio 4 447 Jan-20-2020, 08:11 PM
Last Post: cimerio
  Doesn't work function pyautogui.typewrite() aliyevmiras 1 575 Dec-22-2019, 11:35 AM
Last Post: aliyevmiras
  For loop in my __init__ doesn't work as expected Jessy 2 303 Nov-18-2019, 10:07 AM
Last Post: buran
  Type function does not work sunnyarora 2 577 Mar-15-2019, 10:50 AM
Last Post: sunnyarora
  Rewrite a function to make it work with 'bottle-pymysql' nikos 1 457 Feb-26-2019, 02:59 PM
Last Post: nikos
  Why this simple function doesnt work? blackknite 8 796 Jan-05-2019, 12:32 PM
Last Post: buran
  Function not returning expected value Euqinu 4 1,057 Sep-10-2018, 12:48 PM
Last Post: Euqinu
  len() function doesn't work with Geany Editor hudabaig 2 1,268 Jun-01-2018, 11:20 PM
Last Post: wavic

Forum Jump:


Users browsing this thread: 1 Guest(s)