Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to pop selected items on a number list

Whatever number the user types, I want this number to be deleted from the list

my_list = []
for i in range(1,21):
    my_list.append(i)
    
delete_numb= int(raw_input("Delete a number: ")

## in here code for delete delete_numb in my_list

I’m sure about this code. But I can’t next step for delete number.
I tried this:

del my_list[delete_numb]
my_list.pop(delete_numb)

These codes are work but not correct. I can’t understand what is theproblem.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have to see index and element in array.

You have to do use remove for delete and element:

my_list = [1, 3, 5, 7, 9]
print "my_list"
  
my_list.remove(3)
my_list.remove(7)
    
print "my_list"

output:

[1, 3, 5, 7, 9]
[1, 5, 9]

If you use del or .pop you must give index number. For my_list = [1, 3, 5, 7, 9]

index 0: 1  
index 1: 3
index 2: 5
index 3: 7
index 4: 9
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading