Jak usunąć element z listy w Pythonie
Listy Pythona mają różne wbudowane metody usuwania elementów z listy. Oprócz tego możemy użyć różnych metod usunięcia elementu z listy poprzez określenie jego pozycji. W tym artykule zostaną omówione różne metody Pythona służące do usuwania elementów z list.
Przykład
Input: ['Rose',' Lily', 'Lotus', 'Sun', 'Sunflower'] Delete: 'Sun' Output: ['Rose',' Lily', 'Lotus', 'Sunflower'] Explanation: In this, we have removed the 'Sun' element from the given list.
Usuń element z listy
Zastosujemy inną metodę usuwania elementów z listy w Pyton :
- Za pomocą Usuń Pythona()
- Za pomocą Pyton
- Korzystanie z Pythona Rozumienie listy
- Za pomocą Pop w Pythonie()
- Za pomocą Odrzuć Pythona()
- Za pomocą Filtr Pythona()
- Za pomocą Krojenie listy Pythona
1. Usuń elementy z listy za pomocą metody usuwania()
Elementy z listy możemy usuwać przekazując wartość usuwanego elementu jako parametr usuwający funkcję ().
Python3
lst> => [> 'Iris'> ,> 'Orchids'> ,> 'Rose'> ,> 'Lavender'> ,> > 'Lily'> ,> 'Carnations'> ]> print> (> 'Original List is :'> , lst)> # using remove()> lst.remove(> 'Orchids'> )> print> (> 'After deleting the item :'> , lst)> |
Wyjście
Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations'] After deleting the item : ['Iris', 'Rose', 'Lavender', 'Lily', 'Carnations']
2. Usuń element z listy za pomocą del()
Możemy usuwać elementy z listy za pomocą Del(). The Pyton instrukcja nie jest funkcją List. Pozycje listy można usunąć za pomocą instrukcji del podając indeks pozycji (elementu), która ma zostać usunięta.
Python3
lst> => [> 'Iris'> ,> 'Orchids'> ,> 'Rose'> ,> 'Lavender'> ,> > 'Lily'> ,> 'Carnations'> ]> print> (> 'Original List is :'> , lst)> # using del statement> # to delete item (Orchids at index 1)> # from the list> del> lst[> 1> ]> print> (> 'After deleting the item :'> , lst)> |
Wyjście
Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations'] After deleting the item : ['Iris', 'Rose', 'Lavender', 'Lily', 'Carnations']
3. Usuń element z listy, korzystając ze zrozumienia listy
Możemy usuwać elementy z listy podczas iteracji. W tej metodzie używamy zrozumienie listy . Tutaj dołączamy wszystkie elementy z wyjątkiem tych, które należy usunąć.
Python3
# Python program to remove given element from the list> list1> => [> 1> ,> 9> ,> 8> ,> 4> ,> 9> ,> 2> ,> 9> ]> > # Printing initial list> print> (> 'original list : '> +> str> (list1))> # using List Comprehension> # to remove list element 9> list1> => [ele> for> ele> in> list1> if> ele !> => 9> ]> > # Printing list after removal> print> (> 'List after element removal is : '> +> str> (list1))> |
Wyjście
original list : [1, 9, 8, 4, 9, 2, 9] List after element removal is : [1, 8, 4, 2]
4. Usuń element z listy za pomocą pop()
Możemy usuwać elementy z listy za pomocą pop(). Funkcja pop() jest także metodą listowania. Możemy usunąć element o określonym indeksie i uzyskać wartość tego elementu za pomocą Muzyka pop() .
Python3
lst> => [> 'Iris'> ,> 'Orchids'> ,> 'Rose'> ,> 'Lavender'> ,> > 'Lily'> ,> 'Carnations'> ]> print> (> 'Original List is :'> , lst)> # using pop() to delete item> # ('Orchids' at index 1) from the list> a> => lst.pop(> 1> )> print> (> 'Item popped :'> , a)> print> (> 'After deleting the item :'> , lst)> |
Wyjście
Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations'] Item popped : Orchids After deleting the item : ['Iris', 'Rose', 'Lavender', 'Lily', 'Carnations']
5. Usuń element z listy za pomocą metody odrzucania()
Możemy usuwać elementy z listy za pomocą metody odrzucania (). W tej metodzie konwertujemy listę na zbiór, a następnie usuwamy element za pomocą funkcji odrzucania (). Następnie konwertujemy zestaw z powrotem na listę.
Python3
# Python program to remove given element from the list> lst> => [> 'Iris'> ,> 'Orchids'> ,> 'Rose'> ,> 'Lavender'> ,> > 'Lily'> ,> 'Carnations'> ]> print> (> 'Original List is :'> , lst)> # using discard() method to remove list element 'orchids'> lst> => set> (lst)> lst.discard(> 'Orchids'> )> # Converting set back to list> lst> => list> (lst)> print> (> 'List after element removal is :'> , lst)> |
Wyjście:
Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations'] List after element removal is : ['Lily', 'Carnations', 'Iris', 'Rose', 'Lavender']
Notatka: Ponieważ lista jest konwertowana do zestawu, wszystkie duplikaty zostaną usunięte i nie będzie można zachować kolejności listy.
6. Usuń element z listy za pomocą filter()
Możemy usuwać elementy z listy za pomocą filter(). W tej metodzie odfiltrowujemy niechciany element z listy za pomocą funkcji filter().
Python3
# Python program to remove given element from the list> lst> => [> 'Iris'> ,> 'Orchids'> ,> 'Rose'> ,> 'Lavender'> ,> > 'Lily'> ,> 'Carnations'> ]> print> (> 'Original List is :'> , lst)> # using discard() method to remove list element 'orchids'> lst1> => filter> (> lambda> item: item!> => 'Orchids'> ,lst)> print> (> 'List after element removal is :'> ,> list> (lst1))> |
Wyjście
Original List is : ['Iris', 'Orchids', 'Rose', 'Lavender', 'Lily', 'Carnations'] List after element removal is : ['Iris', 'Rose', 'Lavender', 'Lily', 'Carnations']
7. Usuń element z listy za pomocą krojenia
Elementy z listy możemy usuwać za pomocą krojenia. Ta metoda tworzy nową listę poprzez pocięcie oryginalnej listy i połączenie części, które nie zawierają usuniętego elementu.
Python3
my_list> => [> 1> ,> 2> ,> 3> ,> 4> ,> 5> ]> my_list> => my_list[:> 2> ]> +> my_list[> 3> :]> print> (my_list)> # Output: [1, 2, 4, 5]> |
Wyjście:
[1, 2, 4, 5]
8. Usuń element z listy za pomocą Itertools
Możemy usuwać elementy z listy za pomocą itertools. W kodzie zastosowano itertools.filterfalse() funkcja usuwająca wszystkie wystąpienia liczby 9 z podanej listy.
Tworzy funkcję lambda, która sprawdza, czy element jest równy 9, i stosuje filtr do listy. Wynikowa przefiltrowana lista jest drukowana jako wynik.
Python3
import> itertools> lst> => [> 1> ,> 9> ,> 8> ,> 4> ,> 9> ,> 2> ,> 9> ]> print> (> 'Original List is :'> , lst)> # itertools.filterfalse() to filter out all occurrences of 9 from the list> lst_filtered> => list> (itertools.filterfalse(> lambda> x: x> => => 9> , lst))> print> (> 'List after element removal is :'> , lst_filtered)> #this code is contributed by Jyothi pinjala.> |
Wyjście
Original List is : [1, 9, 8, 4, 9, 2, 9] List after element removal is : [1, 8, 4, 2]
W tym artykule omówiliśmy różne metody usuwania pozycji z listy. W tym artykule wymieniono łącznie 8 metod. Usunięcie elementu z listy można wykonać za pomocą wbudowanych funkcji, ale wykorzystaliśmy także metody niekonwencjonalne.
Podobne lektury:
- Usuń dany element z listy
- Sposoby usunięcia konkretnego elementu listy
- Usuń pierwszy element listy
- Usuń wiele elementów z listy w Pythonie