0

Python code:

PeoplesNames = []
while len(PeoplesNames) < 3:
    person = input('Enter your name: ')
    PeoplesNames.append(person)
print PeoplesNames
if 'Dan' in PeoplesNames:
    PeoplesNames.pop('Dan')
    print PeoplesNames

From my understanding this should run through the while loop (which it does) until the list hits length of 3 (which it does) then print the list (which it does) then hit the if statement and delete dan from the list and then print the new list (which it does not) do i need to nest the if statement or something else? thanks

4
  • 1
    look up the difference between .remove() and .pop() for lists: docs.python.org/2/tutorial/datastructures.html Commented Oct 7, 2015 at 10:26
  • sorry yes it is remove that works Commented Oct 7, 2015 at 10:32
  • No ; required in Python. Commented Oct 7, 2015 at 10:35
  • This should raise an exception which makes it clear on which line the error is. Are you just ignoring the exception? Commented Oct 7, 2015 at 11:43

2 Answers 2

2
list.pop() #used to pop out the top of the stack(list) or

list.pop(index) #can be an index that needed to be pop out, but

list.remove(item) # removes the item specified

Try with the below solution

if 'Dan' in PeoplesNames:
        PeoplesNames.remove('Dan')
        print PeoplesNames
Sign up to request clarification or add additional context in comments.

1 Comment

list.pop does take an argument; but it’s an index.
1

or - keeping EAFP in mind - you could:

PeoplesNames = [];
while len(PeoplesNames) < 3:
    person = raw_input('Enter your name: ')
    PeoplesNames.append(person)
print PeoplesNames
try:
    PeoplesNames.remove('Dan')
except ValueError:
    pass
print PeoplesNames

also note that in python 2.7 you need to use raw_input() instead of input().

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.