-1

Get the following error in code, unsure of what it means or what I did wrong. Just trying to initialize three list values to empty collections:

nba,nfl,mlb = []
ValueError: not enough values to unpack (expected 3, got 0)
dang
  • 118
  • 1
  • 10
  • What did you find when you searched for information about the error? What about that information seemed insufficient or was confusing? – jpmc26 Aug 24 '18 at 20:43
  • @jpmc26 there was not sufficient material on a google search for initializing multiple lists in a single line. I'm sure you'll _stand – dang Aug 27 '18 at 19:41
  • That is simply not true. https://www.google.com/search?q=python+create+multiple+lists+in+one+line&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:unofficial And the first result: https://stackoverflow.com/q/2402646/1394393 The second answer contains exactly the syntax you accepted. – jpmc26 Aug 27 '18 at 19:57

4 Answers4

2

The issue is , left hand side values are not enough to assign to the number of variable on the left so instead do

nba,nfl,mlb = [],[],[]
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
1

This attempts to unpack, as the error message says, an iterable on the right hand side into three variables on the left hand side, so, for example, after running a,b,c = 1,2,3 you get a == 1 and b == 2 and c == 3.

In your case, this iterable is empty, so "there are not enough values to unpack": there are three variables, but no values in the iterable (the iterable is an empty list). What you need is the following:

a,b,c = [],[],[]

Here you have three variables a,b,c and the iterable discussed above is the tuple [],[],[] in this case.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
1

Another option if you want to unpack a generator for instance:

nba,nfl,mlb = [[] for _ in range(3)]
datawrestler
  • 1,527
  • 15
  • 17
1

basically means left hand side has more values than right hand side of =

nba = nfl = mlb = [] should get you three list values initialized to empty collections. So should nba, nfl, mlb = [], [], []

k.wahome
  • 962
  • 5
  • 14