user3056783
user3056783

Reputation: 2626

Can't remove item from list in Python - where's the catch?

I'm a newbie so please bear with me:

import random

directions = ['north', 'east', 'south', 'west']
bad_directions = directions[:]

good_direction = random.sample(directions,1)
good_direction = str(good_direction).replace('[','').replace(']','')

bad_directions.remove(good_direction)
print bad_directions

This raises ValueError:

Traceback (most recent call last):
  File "directions.py", line 9, in <module>
    bad_directions.remove(good_direction)
ValueError: list.remove(x): x not in list

I tried checking the types of "good_direction" and "bad_directions[1]" just to see if they're the same and they're both string.

Upvotes: 0

Views: 98

Answers (6)

Daan Timmer
Daan Timmer

Reputation: 15047

The deepcopy is there to make an exact deepcopy version of directions. I don't know if you need it, I just added it.

https://ideone.com/9D8FZI

# your code goes here
import random
import copy

directions = ['north', 'east', 'south', 'west']

bad_directions = copy.deepcopy(directions)

good_directions = random.choice(directions)
bad_directions.remove(good_directions)

print good_directions,bad_directions

If you don't need the deepcopy, then you also don't need to keep directions the original list. It can then be made easier as follows:

https://ideone.com/C49ziQ

# your code goes here
import random

bad_directions = ['north', 'east', 'south', 'west']

good_directions = random.choice(bad_directions)
bad_directions.remove(good_directions)

print good_directions,bad_directions

Upvotes: 1

snobb
snobb

Reputation: 1074

You can do that differently:

bad_directions.pop(random.randrange(len(bad_directions)))

or

del(bad_directions[random.randrange(len(bad_directions))])

I agree with previous post - it does look like a massive overkill to convert list to string then normalize and then work with it.

Upvotes: 0

Gassa
Gassa

Reputation: 8846

Here is another way to do that:

import random

directions = ['north', 'east', 'south', 'west']
index = random.randrange (len (directions)) # number 0, 1, 2 or 3
good_direction = directions[index]
bad_directions = directions[:index] + directions[index + 1:] # all but directions[index]
print good_direction
print bad_directions

An example output is:

south
['north', 'east', 'west']

You can discard the lines for finding good_direction if you need only bad_directions.

Upvotes: 0

durga
durga

Reputation: 434

good directions per your code returns a different string than whats in the directions list. Below is the output.

>>> good_direction
"'east'"
>>> good_direction
"'east'"
>>> good_direction in directions
False

——— may be the below peice of code will achieve what you are trying to achieve.

>>> good_direction = random.choice(directions)
>>> good_direction
'east'
>>> bad_directions.remove(good_direction)
>>> print bad_directions
['north', 'south', 'west']

Upvotes: 1

Paulo Bu
Paulo Bu

Reputation: 29794

I think this line is overkill and error prone:

good_direction = str(good_direction).replace('[','').replace(']','')

If you want to retrieve the string returned by random.sample(directions,1) you can just do:

good_direction=random.sample(directions,1)[0]

Your code is failing because you're missing to replace something from the retrieved string.

I followed your code and the resulting string after the replacement was "'abc'":

>>>import random
>>>l=['abc' for i in range(10)]
>>>s=random.sample(l,1)
>>>s
['abc']
>>>str(s)
"['abc']"
>>>s1=str(s).replace('[','').replace(']','')
>>>s1
"'abc'"    # auch! the single quotes remain there!
>>>s1 in l
False

Upvotes: 2

James Sapam
James Sapam

Reputation: 16940

Please put [0] here :

good_direction = random.sample(directions,1)[0]

Upvotes: 2

Related Questions