3

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)

Carlo Zanocco
  • 1,967
  • 4
  • 18
  • 31
Rob Friedman
  • 95
  • 2
  • 8

2 Answers2

4

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
Nouman
  • 6,947
  • 7
  • 32
  • 60
0

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...

Fabrice LARRIBE
  • 322
  • 3
  • 9