Problem Removing list items in a for loop (python)

Possible duplicate:
Remove items from a list iterating in Python

Hi, I have a problem, I cannot delete items during iteration through a list in python, Here's what I got: the header should be removed if the user enters n or N when asking a delete question in a for loop, the problem is that when all this is done, all the elements are still there, and not one of them has been deleted ...

titles_list = ["English", "Math", "History", "IT", "Biology", "Spanish"] for title in titles_list: print "Do you want to keep the title:", title , "\n or Delete it ? Input Y for keep, N for Delete " Question = raw_input() if str(Question.upper) == "N": titles_list.remove(title) print titles_list 
+6
python list loops
source share
4 answers

The code below will fix your problem. You must iterate over a copy of the list. You cannot remove items from the list that you are executing.

 import copy def main(): titles_list = ["English", "Math", "History", "IT", "Biology", "Spanish"] titles_list_orig = copy.deepcopy(titles_list) for title in titles_list_orig: print "Do you want to keep the title:", title , "\n or Delete it? Input Y for keep, N for Delete " Question = raw_input() if str(Question.upper()) == "N": titles_list.remove(title) print titles_list 
+10
source share

There are two main problems in your code.

First, you do not call the upper method, but simply refer to it. You should call it (via Question.upper() ), as w00t does in your answer.

Putting some diagnostic print statements inside your loop would be a good way to see this (especially listing str(Question.upper) ) (Tangent: Question is a bad name for a variable that contains the answer to a question about the program asked by the user)

Secondly, deleting already seen items from the list that you are repeating will skip the values. In fact, you do not need to copy the entire list to deal with this - just repeating it otherwise is enough to fix the problem.

Finally, a couple of minor cosmetic points is that raw_input() takes a prompt argument, so you don’t need a separate print statement, and calling upper() on the line itself will always return the line:

 titles_list = ["English", "Math", "History", "IT", "Biology", "Spanish"] prompt = ("Do you want to keep the title: {}\n" "or Delete it? Input Y for keep, N for Delete: ") for title in reversed(titles_list): answer = raw_input(prompt.format(title)) if answer.upper() == "N": titles_list.remove(title) print titles_list 
+7
source share

I think the main problem in your code was the misuse of the top function. Once you fix it, you can remove the headers from the list as you wish. You can use an index or value. Here is the code that worked for me

  #! / usr / bin / python

 import string

 titles_list = ["English", "Math", "Biology", "IT", "History"]
 for title in titles_list:
   answer = raw_input ("Do you want to keep this title% s, type y or n \ n"% (title))
   if string.upper (answer) == "N":
     # i = titles_list.index (title)
     # del titles_list [i]
     titles_list.remove (title)
     print "now list is", titles_list
 print titles_list

Please view the commented lines using the index. In addition, you can make the code shorter with the raw_input (prompt) function.

You also need to think about a scenario in which there are multiple occurrences of the same headings in your list, in which case I suggest getting all the indexes for the heading until the list becomes empty and removes the headings using del (index) since the solutions above remove only the first occurrence of the name.

+1
source share

I know that there is an answer already, here is another way to complete and demonstrate understanding of the list.

This method takes a list, asks to save each item, and returns a new list, with the exception of those marked for deletion:

 def delete_items(titles): deleted_items = [] for title in titles: print('keep %s?' % title) if str(raw_input().upper()) == 'N': deleted_items.append(title) return [e for e in titles if e not in deleted_items] 
0
source share

All Articles