“Python entfernen Sie Element aus der Liste” Code-Antworten

Python entfernen

# 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

Listen Sie ein Element auf

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

Entfernen Sie ein Element aus einer Python -Liste mit der Entre -() -Methode

# 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 entfernen Sie Element aus der Liste

myList.remove(item)
Blushing Booby

Python entfernen Sie das Element in der 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

Wert aus der Liste Python entfernen

# 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

Ähnliche Antworten wie “Python entfernen Sie Element aus der Liste”

Fragen ähnlich wie “Python entfernen Sie Element aus der Liste”

Weitere verwandte Antworten zu “Python entfernen Sie Element aus der Liste” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen