2

I am trying to create 10 rooms that have a random number of boxes and apples. When I try to display them I get this error: "IndexError('list index out of range',)" Also, I am looking for the best method to handle multiple dictionaries and work on them. This is my code:

rooms=[]
for i in range(1,11):
    a={'apples':random.randint(0,3),'box':random.randint(0,2)}
    rooms.append(dict(a))
for i in range(1,11):
    print(rooms[i])
  • 4
    In python lists are zero indexed. Replace both `range(1, 11)` with `range(10)` – awesoon Oct 23 '18 at 13:01
  • When iterating over the contents in a list, just do `for item in rooms: print(item)`. (That would work for you bottom loop) – N Chauhan Oct 23 '18 at 13:21
  • `rooms[10]` does not exists as last index would be 9 and not 10.As commented by other changing to range(10) should solve the problem – mad_ Oct 23 '18 at 13:26
  • Possible duplicate of [IndexError: list index out of range and python](https://stackoverflow.com/questions/1098643/indexerror-list-index-out-of-range-and-python) – U13-Forward Oct 24 '18 at 06:11

6 Answers6

0

It doesn't work here because indexes are from 0 to 9 on your list of dicts and you ask for the dict at the index 10

Replace both range(1,11) by range(10) and it will work.

0

The issue is that you are using:

for i in range(1,11)

to iterate through your list. Your list of rooms has a length of 10. However, you are looking to iterate through a 11th item, which of course does not exist (remember that python uses 0 indexing)

Instead, use:

for i in range(10)
Dominique Paul
  • 1,623
  • 2
  • 18
  • 31
0
import random
rooms=[]
for i in range(1,11):
    a={'apples':random.randint(0,3),'box':random.randint(0,2)}
    rooms.append(dict(a))
for i in range(0,10):
    print(rooms[i])

EDIT 1: When you are creating rooms list using first loop for i in range(1,11) but in rooms list indexing start from 0 so rooms list contain item from index 0 to 10. So while printing the list after appending dict to it you have to start indexing from 0 to the length of list.

Always check first length of list and starting and ending index of list and then start printing elements This method you write is applicable when you modify existing list on some particular index. So take care of indexing while working with list.

Rohit-Pandey
  • 2,039
  • 17
  • 24
  • Thank you for the code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its [long-term value](https://meta.stackexchange.com/q/114762/206345) by describing why this is a good solution to the problem, and would make it more useful to future readers with other similar questions. Please edit your answer to add some explanation, including the assumptions you've made. – sepehr Oct 23 '18 at 15:14
  • 1
    @sepehr Understand. – Rohit-Pandey Oct 24 '18 at 05:46
0

Its showing error because rooms does not have any index '10'

there is a better way to do it using list comprehension

import random

rooms=[{'apples':random.randint(0,3),'box':random.randint(0,2)} for i in range(10)]

for d in rooms:
    print(d)

also if you are creating 10 rooms then you can do range(10) there is no need of range(1, 11)

ansu5555
  • 416
  • 2
  • 7
0

To iterate over all the elements in a collection, you can use the following:

for room in rooms:
    print(room)

When you iterate with the range(...), you're effectively doing the same thing with a collection of numbers. You can think of it as this:

for index in [1,2,3,...11]:
    ...

The first way expresses what you want more directly, and will avoid the off-by-one error with your range statement.

jelford
  • 2,625
  • 1
  • 19
  • 33
0
for i in range(1,11):
    print(rooms[i])

Is the problem.

Python indexing starts from zero.

So it goes like (0,1,2,3, and so on..)

So you have to do:

for i in range(10):
    print(rooms[i])

Or subtract 1 from i:

for i in range(1,11):
    print(rooms[i-1])

Or just go over the list:

for i in rooms:
    print(i)
U13-Forward
  • 69,221
  • 14
  • 89
  • 114