Professional Documents
Culture Documents
Python
Python
We can delete one or more items from a list using the keyword del. It can
even delete the list entirely.
my_list = ['p','r','o','b','l','e','m']
# delete one item
del my_list[2]
# Output: ['p', 'r', 'b', 'l', 'e', 'm']
print(my_list)
# delete multiple items
del my_list[1:5]
# Output: ['p', 'm']
print(my_list)
# delete entire list
del my_list
# Error: List not defined
print(my_list)
We can use remove() method to remove the given item or pop() method to remove
an item at the given index.
The pop() method removes and returns the last item if index is not provided. This
helps us implement lists as stacks (first in, last out data structure).
We can also use the clear() method to empty a list.
my_list = ['p','r','o','b','l','e','m']
my_list.remove('p')
# Output: ['r', 'o', 'b', 'l', 'e', 'm']
print(my_list)
# Output: 'o'
print(my_list.pop(1))
# Output: ['r', 'b', 'l', 'e', 'm']
print(my_list)
# Output: 'm'
print(my_list.pop())
# Output: ['r', 'b', 'l', 'e']
print(my_list)
my_list.clear()
# Output: []
print(my_list)
Finally, we can also delete items in a list by
assigning an empty list to a slice of elements.
# an element is added
animal.append('guinea pig')
#Updated List
print('Updated animal list: ', animal)
# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# index is printed
print('The index of p:', index)
# random list
random = ['a', ('a', 'b'), [3, 4]]
# index is printed
print("The index of ('a', 'b'):", index)
# index is printed
print("The index of [3, 4]:", index)
# language list
language = ['French', 'English', 'German']
language.extend(language1)
# Extended List
print('Language List: ', language)
# language list
language = ['French', 'English', 'German']
# language tuple
language_tuple = ('Spanish', 'Portuguese')
# language set
language_set = {'Chinese', 'Japanese'}
# number tuple
number_tuple = (3, 4)
# animal list
animal = ['cat', 'dog', 'dog', 'guinea pig', 'dog']
# print count
print('The count of i is:', count)
# print count
print('The count of p is:', count)
# random list
random = ['a', ('a', 'b'), ('a', 'b'), [3, 4]]
# print count
print("The count of ('a', 'b') is:", count)
# print count
print("The count of [3, 4] is:", count)
# programming language list
language = ['Python', 'Java', 'C++', 'French', 'C']
# Updated List
print('Updated List: ', language)
# programming language list
language = ['Python', 'Java', 'C++', 'Ruby', 'C']
# When -1 is passed
# Pops Last Element
print('\nWhen -1 is passed:')
print('Return Value: ', language.pop(-1))
print('Updated List: ', language)
# When -3 is passed
# Pops Third Last Element
print('\nWhen -3 is passed:')
print('Return Value: ', language.pop(-3))
print('Updated List: ', language)
# Operating System List
os = ['Windows', 'macOS', 'Linux']
print('Original List:', os)
# List Reverse
os.reverse()
# updated list
print('Updated List:', os)
# Operating System List
os = ['Windows', 'macOS', 'Linux']
print('Original List:', os)
# Reversing a list
#Syntax: reversed_list = os[start:stop:step]
reversed_list = os[::-1]
# updated list
print('Updated List:', reversed_list)
# Operating System List
os = ['Windows', 'macOS', 'Linux']
# print vowels
print('Sorted list:', vowels)
# take second element for sort
def takeSecond(elem):
return elem[1]
# random list
random = [(2, 2), (3, 4), (4, 1), (1, 3)]
# print list
print('Sorted list:', random)
old_list = [1, 2, 3]
new_list = old_list
# copying a list
new_list = list.copy()
print('List:', list)
What is a set in Python?
#my_set[0]
# add an element
# Output: {1, 2, 3}
my_set.add(2)
print(my_set)