why ? ( list index out of range )

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1860 Views - Last Post: 13 December 2012 - 10:20 AM Rate Topic: -----

#1 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8902
  • View blog
  • Posts: 33,382
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 fledgeling  Icon User is offline

  • D.I.C Head

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

Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:23 PM

View Postmodi123_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 ?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8902
  • View blog
  • Posts: 33,382
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 fledgeling  Icon User is offline

  • D.I.C Head

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

Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:28 PM

View Postmodi123_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 ?
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8902
  • View blog
  • Posts: 33,382
  • 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?
Was This Post Helpful? 0
  • +
  • -

#7 fledgeling  Icon User is offline

  • D.I.C Head

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

Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:32 PM

View Postmodi123_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
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8902
  • View blog
  • Posts: 33,382
  • 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.
Was This Post Helpful? 0
  • +
  • -

#9 fledgeling  Icon User is offline

  • D.I.C Head

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

Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:39 PM

View Postmodi123_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 ?
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,681
  • 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

Was This Post Helpful? 1
  • +
  • -

#11 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8902
  • View blog
  • Posts: 33,382
  • 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) :D

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

Was This Post Helpful? 0
  • +
  • -

#12 fledgeling  Icon User is offline

  • D.I.C Head

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

Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:49 PM

View Postjon.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)?
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,681
  • Joined: 19-March 11

Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:50 PM

View Postfledgeling, 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.
Was This Post Helpful? 0
  • +
  • -

#14 fledgeling  Icon User is offline

  • D.I.C Head

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

Re: why ? ( list index out of range )

Posted 12 December 2012 - 12:52 PM

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

View Postfledgeling, 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 :)
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,681
  • 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

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2