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.
>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