“Python supprimer l'élément de la liste” Réponses codées

Python supprimer

# the list.remove(object) method takes one argument 
# the object or element value you want to remove from the list
# it removes the first coccurence from the list

generic_list = [1, 2, 2, 2, 3, 3, 4, 5, 5, 5, 6, 8, 8, 8]

generic_list.remove(1)

# The Generic list will now be:
# [2, 2, 2, 3, 3, 4, 5, 5, 5, 6, 8, 8, 8]

McBurd

Liste de supprimer un élément

a = [3,4,5]
del a[2]
print(a)
# [3, 4]
Sore Sloth

Supprimer un élément d'une liste de python à l'aide de la méthode supprime ()

# Python program to demonstrate
# Removal of elements in a List
 
# Creating a List
List = [1, 2, 3, 4, 5, 6,
        7, 8, 9, 10, 11, 12]
print("Initial List: ")
print(List)
 
# Removing elements from List
# using Remove() method
List.remove(3)
List.remove(8)
print("\nList after Removal of two elements: ")
print(List)
 
# Removing elements from List
# using iterator method
for i in range(1, 3):
    List.remove(i)
print("\nList after Removing a range of elements: ")
print(List)
Outrageous Ostrich

Python supprimer l'élément de la liste

myList.remove(item)
Blushing Booby

Python supprime l'élément dans la liste

my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya']
my_list.remove(12) # it will remove the element 12 at the start.
print(my_list)
Thoughtless Tiger

Suppression de la valeur de la liste Python

# create a list
prime_numbers = [2, 3, 5, 7, 9, 11]
# remove 9 from the list using 'remove' method
prime_numbers.remove(9)
# prime_numbers are automatically updated. No seperate initialisation is required
print('Updated List: ', prime_numbers)
# Output: Updated List:  [2, 3, 5, 7, 11]

# remove 9 from the list using 'pop' method
# remove 9 from the list and returns the value 9. 
# We need to mention the position of the item that needs to be removed instead of the actual value.
prime_numbers.pop(4)
# returns/displays 9
# prime_numbers are automatically updated. No seperate initialisation is required
print('Updated List: ', prime_numbers)
# Output: Updated List:  [2, 3, 5, 7, 11]
Tame Tarantula

Réponses similaires à “Python supprimer l'élément de la liste”

Questions similaires à “Python supprimer l'élément de la liste”

Plus de réponses similaires à “Python supprimer l'élément de la liste” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code