Lists are one of the most useful data types in Python. They allow you to store many values in one place and work with them in an organized way. In this section, you will learn how to copy lists, slice them, remove parts of them, and check whether items are present.
When you write:
vehicles_one = ['car', 'bicycle', 'motor']
vehicles_two = vehicles_oneboth variables point to the same list in memory. This means that changing one list also changes the other.
vehicles_one = ['car', 'bicycle', 'motor']
vehicles_two = vehicles_one
del vehicles_one[0]
print(vehicles_two) # outputs: ['bicycle', 'motor']=does not create a new list.- It creates another name for the same list.
If you want a true copy, use slicing.
colors = ['red', 'green', 'orange']
copy_whole_colors = colors[:] # copy the entire list
copy_part_colors = colors[0:2] # copy part of the listA copied list is independent from the original. Changing one will not affect the other.
You can also use negative numbers to count from the end of the list.
sample_list = ['A', 'B', 'C', 'D', 'E']
new_list = sample_list[2:-1]
print(new_list) # outputs: ['C', 'D']The basic format is:
list[start:end]You may omit one or both values:
my_list = [1, 2, 3, 4, 5]
slice_one = my_list[2:] # from index 2 to the end
slice_two = my_list[:2] # from the beginning up to index 2
slice_three = my_list[-2:] # last two items
print(slice_one) # outputs: [3, 4, 5]
print(slice_two) # outputs: [1, 2]
print(slice_three) # outputs: [4, 5]You can remove a section of a list using del.
my_list = [1, 2, 3, 4, 5]
del my_list[0:2]
print(my_list) # outputs: [3, 4, 5]
del my_list[:]
print(my_list) # outputs: []These operators help you test whether an item exists in a list.
my_list = ['A', 'B', 1, 2]
print('A' in my_list) # outputs: True
print('C' not in my_list) # outputs: True
print(2 not in my_list) # outputs: False- Use
inwhen you want to check if something exists. - Use
not inwhen you want to check if something does not exist.
=creates another reference to the same list.[:]creates a copy of a list.- Slicing helps you access or remove part of a list.
inandnot incheck membership.
Try to answer these questions on your own before checking the solutions.
What is the output of the following code?
list_1 = ['A', 'B', 'C']
list_2 = list_1
list_3 = list_2
del list_1[0]
del list_2[0]
print(list_3)What is the output of the following code?
list_1 = ['A', 'B', 'C']
list_2 = list_1
list_3 = list_2
del list_1[0]
del list_2
print(list_3)What is the output of the following code?
list_1 = ['A', 'B', 'C']
list_2 = list_1
list_3 = list_2
del list_1[0]
del list_2[:]
print(list_3)What is the output of the following code?
list_1 = ['A', 'B', 'C']
list_2 = list_1[:]
list_3 = list_2[:]
del list_1[0]
del list_2[0]
print(list_3)Replace ??? with the correct operator so that the code produces the expected result.
my_list = [1, 2, 'in', True, 'ABC']
print(1 ??? my_list) # outputs True
print('A' ??? my_list) # outputs True
print(3 ??? my_list) # outputs True
print(False ??? my_list) # outputs False