Reputation: 95
I have two lists.
d1 = ["'02/01/2018'", "'01/01/2018'", "'12/01/2017'"]
d2 = ["'02/28/2018'", "'01/31/2018'", "'12/31/2017'"]
I am trying to get these values to be unpacked in a for loop.
for i,y in d1,d2:
i,y = Startdate, Enddate
I realize this iteration will overwrite the values for Startdate and Enddate with each iteration but for now i'm just trying to successfully unpack the elements of each list.
I get the following error:
too many values to unpack (expected 2)
I thought I was unpacking 2? (d1 and d2)
Upvotes: 3
Views: 3284
Reputation: 7313
You need to use zip
. Here is an experiment with zip
:
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> for i,y in zip(a,b):
print(i,y)
1 4
2 5
3 6
>>>
You can say that your loop can be like:
for i,y in zip(d1,d2):
i,y = Startdate, Enddate
Upvotes: 4
Reputation: 332
The for loop cannot "unpack" several list as you tried in you example, but you can 'zip' it as mentionner by @Nouman
list(zip([1, 2, 3], ['a', 'b', 'c'])) --> [(1, 'a'), (2, 'b'), (3, 'c')]
You can now unpack the dates two per two...
Upvotes: 0