# why ? ( list index out of range )

• (2 Pages)
• 1
• 2

## 19 Replies - 4880 Views - Last Post: 13 December 2012 - 10:20 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=303752&amp;s=3f928fe6065eaa9dd0071e90f2d3fb1e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 fledgeling

Reputation: 1
• Posts: 69
• Joined: 10-October 12

# why ? ( list index out of range )

Posted 12 December 2012 - 12:18 PM

Well guys here is my code, i know its incomplete and there is a never ending loop but my problem is that from time to time i get error ( list index out of range) why ?

```import random

lists=[[150, 9, 'drop'], [35,13,"cage"],[200,153,"lust"],[160,50,"google"],[60,15,"gun"],[45,68,"razor"],[60,27,"armor"] ]
x=random.sample(lists,  3)
theitems=[]
for i in range(len(x)):
theitems.append(x[i][2])
print theitems
tw=0
tv=0
while True:

epilogi=raw_input("choose")
for i in range(len(theitems)):

if theitems[i]==epilogi:

tw=tw+x[i][1]
tv=tv+x[i][0]
theitems.remove(theitems[i])
x.remove(x[i])
print theitems,x

```

Is This A Good Question/Topic? 0

## Replies To: why ? ( list index out of range )

### #2 modi123_1

• Suitor #2

Reputation: 14425
• Posts: 57,820
• Joined: 12-June 08

## Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:20 PM

What are the upper and lower index of your array? What are the upper and lower bounds in the for loop?

### #3 fledgeling

Reputation: 1
• Posts: 69
• Joined: 10-October 12

## Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:23 PM

modi123_1, on 12 December 2012 - 12:20 PM, said:

What are the upper and lower index of your array? What are the upper and lower bounds in the for loop?

i think that in these for loops is line range(3) so 0 to 2 ?

### #4 modi123_1

• Suitor #2

Reputation: 14425
• Posts: 57,820
• Joined: 12-June 08

## Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:25 PM

Are you sure your array counts from 0 to 2? Are you sure it's not 0 to 3?

### #5 fledgeling

Reputation: 1
• Posts: 69
• Joined: 10-October 12

## Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:28 PM

modi123_1, on 12 December 2012 - 12:25 PM, said:

Are you sure your array counts from 0 to 2? Are you sure it's not 0 to 3?

the len(theitems) = 3 , isnt it ?

### #6 modi123_1

• Suitor #2

Reputation: 14425
• Posts: 57,820
• Joined: 12-June 08

## Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:29 PM

Well I am asking you - so you tell me. What are the values of 'i' when executed?

### #7 fledgeling

Reputation: 1
• Posts: 69
• Joined: 10-October 12

## Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:32 PM

modi123_1, on 12 December 2012 - 12:29 PM, said:

Well I am asking you - so you tell me. What are the values of 'i' when executed?

it is supposed to be 0,1,2 but i dont know if i have achieved this in the program, maybe thats the mistake

### #8 modi123_1

• Suitor #2

Reputation: 14425
• Posts: 57,820
• Joined: 12-June 08

## Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:36 PM

Arrays start at 0, right? so an array the length of 3 would have indexes like"
[0]
[1]
[2]

Right?

Your for loop says what? "Start at 0 and go to the array's length" right? If you said the length was 3 that means the values of i are: 0, 1, 2, 3. Is 3 a valid index of the array (above) in this example? No, no it is not. It is out of the bounds of the array.

### #9 fledgeling

Reputation: 1
• Posts: 69
• Joined: 10-October 12

## Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:39 PM

modi123_1, on 12 December 2012 - 12:36 PM, said:

Arrays start at 0, right? so an array the length of 3 would have indexes like"
[0]
[1]
[2]

Right?

Your for loop says what? "Start at 0 and go to the array's length" right? If you said the length was 3 that means the values of i are: 0, 1, 2, 3. Is 3 a valid index of the array (above) in this example? No, no it is not. It is out of the bounds of the array.

so i should put range(len(theitems)-1) ?
would that work ?

### #10 jon.kiparsky

• Beginner

Reputation: 11224
• Posts: 19,242
• Joined: 19-March 11

## Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:46 PM

I see what you're after, modi, and I don't think that's the issue.
```for i in range(3):\
print i
```

prints 0, 1, 2, as you'd want, so that's fine.

I think the problem is that you're removing an item from the list as you iterate over it. That's called a concurrent modification, and it's probably what's causing your problem.

Let's walk through it. I have a list l=[1,2,3,4]. I want to remove even items from the list, so I walk through the list, and for each item in the list, I remove it if it's even.

```for i in range (len(l)):
if l[i]%2==0:
l.remove(l[i])
```

What happens? Why? Hint: how many times is range(len(l)) evaluated?

In python, you seldom want to iterate over a list by index. Instead, just iterate over the items themselves:

```for item in l:
handle(item)
```

This post has been edited by jon.kiparsky: 12 December 2012 - 12:47 PM

### #11 modi123_1

• Suitor #2

Reputation: 14425
• Posts: 57,820
• Joined: 12-June 08

## Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:48 PM

How about you try it out and see what happens! Testing things out is a good learning mechanism!

@jon: pffssh.. I was going to get there.. (eventually)

This post has been edited by modi123_1: 12 December 2012 - 12:49 PM

### #12 fledgeling

Reputation: 1
• Posts: 69
• Joined: 10-October 12

## Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:49 PM

jon.kiparsky, on 12 December 2012 - 12:46 PM, said:

I see what you're after, modi, and I don't think that's the issue.
```for i in range(3):\
print i
```

prints 0, 1, 2, as you'd want, so that's fine.

I think the problem is that you're removing an item from the list as you iterate over it. That's called a concurrent modification, and it's probably what's causing your problem.

Let's walk through it. I have a list l=[1,2,3,4]. I want to remove even items from the list, so I walk through the list, and for each item in the list, I remove it if it's even.

```for i in range (len(l)):
if l[i]%2==0:
l.remove(l[i])
```

What happens? Why? Hint: how many times is range(len(l)) evaluated?

oh you mean its because i change the length as i go on ?
so i would solve it by doing something like
o=len(list)
for i in range (o)?

### #13 jon.kiparsky

• Beginner

Reputation: 11224
• Posts: 19,242
• Joined: 19-March 11

## Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:50 PM

fledgeling, on 12 December 2012 - 02:49 PM, said:

oh you mean its because i change the length as i go on ?
so i would solve it by doing something like
o=len(list)
for i in range (o)?

That's the problem, but that's not the solution.

Consider this:
```>>> for i in l:
...   if i%2==0: l.remove(i)

```

Try it in your interpreter and see if it works better than the other version.

### #14 fledgeling

Reputation: 1
• Posts: 69
• Joined: 10-October 12

## Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:52 PM

jon.kiparsky, on 12 December 2012 - 12:50 PM, said:

fledgeling, on 12 December 2012 - 02:49 PM, said:

oh you mean its because i change the length as i go on ?
so i would solve it by doing something like
o=len(list)
for i in range (o)?

That's the problem, but that's not the solution.

Consider this:
```>>> for i in l:
...   if i%2==0: l.remove(i)

```

Try it in your interpreter and see if it works better than the other version.

Ok i will but i really must leave my computer now, i will test it later and post again , Thank you very much the help of both of you is apreciated

### #15 jon.kiparsky

• Beginner

Reputation: 11224
• Posts: 19,242
• Joined: 19-March 11

## Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:55 PM

No sweat. Best of luck!
As you get the hang of this, you might find that it works better to do this in a list comprehension or a filter - those seem more "pythonic" approaches to this sort of problem - but for now, just getting away from the iteration by index will help a lot.

This post has been edited by jon.kiparsky: 12 December 2012 - 12:57 PM