[Python Basics] Working With Lists — Part 3
We practice how to work with part of a list. The topics are slicing, looping over a slice and copying a list.
If you cannot read it, click my Friend’s Link!
We already know what the Python list is and how to work with only part of a list and even how to copy it. Do we?
For many of you, it is true. But there is still a growing number of new generations who want to participate in this fascinating new world of machine learning, artificial intelligence, data science or whatever. We should provide them some materials to read, think and learn.
This post is an attempt to prepare teaching materials to my students whose major is not computer science, but who are willing to learn this new fascinating world.
Working with Part of a List
We know how to access single elements in a list and how to work over all elements in a list. Now it is time to work with a specific part of a list, which Python calls a slice.
Slicing a List
To work with a specific group of items in a list, we need to specify the index of the first and the last elements we want to work with. As with the range()
function, Python stops one item before the second parameter we specify.
Take an example.
my_string = “Hello, Python!”
my_list = list(my_string)print(my_list[0:3]) #1
The code at #1 prints a slice of the list, which includes the first 3 letters: ‘H’, ‘e’ and ‘l’. Note that the slicing requires square brackets [ ]
. Also, the slicing requires a colon (:)
to separate parameters, while the range()
function requires a comma (,)
to separate parameters.
The protocol of slicing is [start:end:increment]
. As usual, the default value of start
is 0 and can be omitted, the default value of increment
is 1 and can also be omitted. The default value of end
is the length of the list and can be omitted, too. Note that end
is the index to be excluded.
Wow, all three parameters can be omitted. Then it is possible to have an expression like my_list[::]
. Let us review all possible combinations of slicings in the next section.
Looping Over a Slice
We can use the slicing in a for
loop. The length of our example string is 14.
my_string = “Hello, Python!”
my_list = list(my_string)for val in my_list[0:14]:
print(val)
This code prints every character including space and punction marks in the variable my_list because the length is 14. Now consider the following slices.
my_list[0:14:1]
my_list[0:14:]
my_list[:14]
my_list[:14:1]
my_list[:14:]
my_list[0:]
my_list[0::1]
my_list[0::]
my_list[::1]
They all represent the same slice!! Take it as an exercise.
Copying a List
Sometimes we need to make a copy of a list and modify it so that we can keep the original list unchanged. To make a copy of an entire list, we can use a slice that includes the entire list by specifying [:]
. This tells Python to make a slice that starts at the default value of start (0) and ends with the default value of end (length), creating a copy of the list.
my_string = "Hello, Python!"
my_list = list(my_string)
my_list_copy = my_list[:]print(my_list)
print(my_list_copy)
This code prints two lists which are the same because the second one is a copy of the first one. Note that these two lists are separated. If you change one of them, that change will not affect the other one.
my_string = "Hello, Python!"
my_list = list(my_string)
my_list_copy = my_list[:]my_list_copy.append('!') # Append one more character to the copy.print(my_list)
print(my_list_copy)
If we ever want to affect both lists, we should connect lists by assigning one to the other. Please note the differences.
my_string = "Hello, Python!"
my_list = list(my_string)my_list2 = my_list # This is a connection.my_list_copy = my_list[:] # This is a copy.