Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for loop - Not enough values to unpack (expected 3, got 2) but I am providing it with 3

def func(a,b,c):
    for x,y,z in a,b,c:
        pass

func(((1,2),(1,3)),((1,4),(1,5)),(1,2))

I expect x,y,z to get the values (1,2), (1,4), and 1. Instead I'm getting an error:

ValueError: not enough values to unpack (expected 3, got 2)
like image 432
ammhyrs Avatar asked Jun 09 '20 04:06

ammhyrs


1 Answers

You need to zip the lists in order to do a for-loop like that without iterating through the arguments passed into func():

def func(a,b,c):
    for x,y,z in zip(a,b,c):
        pass

func(((1,2),(1,3)),((1,4),(1,5)),(1,2))

Otherwise, the for-loop will iterate through every argument passed into func.

like image 95
Ann Zen Avatar answered Oct 02 '22 12:10

Ann Zen