# why ? ( list index out of range )

• (2 Pages)
• 1
• 2

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

### #16 fledgeling

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

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

Posted 13 December 2012 - 06:37 AM

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

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.

I tookunder consideration all you said about indexes so what i made is this one, i think it works but i would love to listen to what you think about it too thanks for all your help

```import random

x=random.sample(lists,  5)
theitems=[]
for i in range(len(x)):
theitems.append(x[i][2])
print theitems
tw=0
tv=0
while True:

epilogi=raw_input("choose")
z=0
for i in theitems:

if i==epilogi:

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

```

### #17 jon.kiparsky

• Beginner

Reputation: 11021
• Posts: 18,804
• Joined: 19-March 11

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

Posted 13 December 2012 - 10:13 AM

Some things improved, some things I'm wondering about.

What is it you're trying to achieve here, anyway?

### #18 fledgeling

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

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

Posted 13 December 2012 - 10:16 AM

jon.kiparsky, on 13 December 2012 - 10:13 AM, said:

Some things improved, some things I'm wondering about.

What is it you're trying to achieve here, anyway?

i am trying to remove from the list these two lists (x and theitems) the index with the word that the user types
Btw, if you have time check my other post about timers in python cause its something i have no clue about

Thannks

### #19 modi123_1

• Suitor #2

Reputation: 13954
• Posts: 55,695
• Joined: 12-June 08

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

Posted 13 December 2012 - 10:17 AM

```lists=[[150, 9, 'drop'], [35,13,"cage"],[200,153,"lust"],[160,50,"google"],[60,15,"gun"],[45,68,"razor"],[60,27,"armor"] ]
```

With a list like that - whew.. either that's a really fun weekend or a nightmare morning.

### #20 fledgeling

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

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

Posted 13 December 2012 - 10:20 AM

modi123_1, on 13 December 2012 - 10:17 AM, said:

```lists=[[150, 9, 'drop'], [35,13,"cage"],[200,153,"lust"],[160,50,"google"],[60,15,"gun"],[45,68,"razor"],[60,27,"armor"] ]
```

With a list like that - whew.. either that's a really fun weekend or a nightmare morning.

it is a part of a knapsack solver the first is the value the second the weight and the last the name of the 'item'

and yeah the item names are random/dump

This post has been edited by fledgeling: 13 December 2012 - 10:21 AM