Skip to content Skip to sidebar Skip to footer

'valueerror: Not Enough Values To Unpack (expected 2, Got 0)'

I am attempting to create list of lists using for loops in python. My plan is to use two separate loops; one loop to create big lists to put the small lists in, and another loop to

Solution 1:

You seem to have a variable called list, which you want to populate with some lists. Your code doesn't work as this syntax is used to extract two pieces of data from the utterable on the right hand side. Clearly, [] is empty, so you can extract nothing out of it.

You could do it like this:

your_list = [list() for x in range(9)] 

Note that you shouldn't call the variable list as there exists a built-in function with the same name that constructs an empty list. Right now the variable makes the built-in unaccessible.

Edit: If you need to have 10 lists of lists:

your_list = [[[] for x in range(9)] for y in range(10)]

Then, your_list will be a list containing 10 lists of lists.

Solution 2:

Better create list with sublists because it is easier to use later,

all_lists = []
for i in range(10):
    all_lists.append([])

or shorter

all_lists = [ [] for i in range(10) ]

And now you can access lists using index, ie.

all_lists[0]

Eventually create dictionary - then you can use number or string as index

all_lists = {}
for i inrange(10):
    all_lists[i] = []  # first version# or 
    all_lists[str(i)] = [] # second version

or shorter

all_lists = { i:[] for i in range(10) } # first versionall_lists = { str(i):[] for i in range(10) } # first version

And now you can access lists using number or string, ie.

all_lists[0]   # first version# or 
all_lists["0"] # second version

EDIT: to create 10x10 grid you can use

grid = [ [None]*10 for _ in range(10) ]

You can use different value instead of None as default.

And to get position (x,y) you can use

grid[y][x]

First is y (row), second is x (column)

Post a Comment for "'valueerror: Not Enough Values To Unpack (expected 2, Got 0)'"