< previous | Table of Contents | next > |
8.4 LIST OPERATIONS
CONCATENATION
Similarly to String concatenation, we can concatenate lists using the +
operator. This allows us to join two or more lists together to form a new list. Note that lists can only be concatenated with other lists. If we tried to combine them with, for example, an integer, the result would be a TypeError
.
list1 = [1, 2, 3] list2 = [4, 5, 6] combined_list = list1 + list2 print(combined_list) # Outputs [1, 2, 3, 4, 5, 6] Note that the new @@combined_list@@ refers to a different location in memory, and hence, if we edited a value of the newly formed list, it would not impact the other lists and vice versa.
REPETITION
Repetition is the process of repeating the elements of a list a specified number of times to create a new list. To do so, we can use the *
operator [64]. During this process, multiple list copies are created and joined together to a single list [65].
list1 = [1, 2, 3] list2 = list1 * 2 list3 = [list1] * 2 print(list2) # Outputs [1, 2, 3, 1, 2, 3] print(list3) # Outputs [[1, 2, 3], [1, 2, 3]]
ACCESSING ELEMENTS THROUGH DIRECT INDEXING
To access a single element in a list, you can use its index, enclosed in square brackets. Python indices start at 0, so the first element is accessed with index 0, the second with 1, and so on. Negative indices can be used to access elements from the end of the list, with -1 being the last element, -2 the second-last, etc.
favorite_songs = ["Without Me", "Valge Mersu", "Tsirkus", "Baby"] print(favorite_songs[0]) # prints out "Without me" which is the first item print(favorite_songs[2]) # prints out "Tsirkus" which is the third item print(favorite_songs[-2]) # prints out "Tsirkus" which is the second to last element
ACCESSING ELEMENTS THROUGH SLICING
Through slicing you can access a subset of a list. The syntax for slicing is list[start:stop:step]
, where start
is the index to start the slice, stop
is the index to end the slice and it is exclusive of the index specified, meaning it won’t be included in the slice, and step
is the interval between elements. When slicing a list, if the start
index is omitted, the slice begins from the start of the list. Conversely, if the stop
index is omitted, the slice extends to the end of the list.
When the step is negative, it reverses the direction in which the slice is taken through the list. This means that instead of reading the list from left to right, the slice is taken from right to left.
favorite_songs = ["Without Me", "Valge Mersu", "Tsirkus", "Baby"] print(favorite_songs[0:2]) # Outputs ["Without Me", "Valge Mersu"] print(favorite_songs[0][2]) # Outputs 't', the third character of the first item in the list print(favorite_songs[::-1]) # reverses the list [“Baby”, "Tsirkus", "Valge Mersu", "Without Me")
KEY THINGS TO REMEMBER
- The
+
operator, concatenates two or more lists into a new list - The
*
operator repeats elements of a list a specified number of times. - Use square brackets with an index to access specific items. The items in a list are indexed starting from 0. Negative indexing starts from the end (-1 for the last item, -2 for the second to last, etc.).
- Slicing allows to access a subset of elements in a list.
start
indicates the start of slicing (inclusive),stop
indicates the index where the slice stops (exclusive),step
is the interval between items. The @@step can be negative for a reversed slice.
SELF-CONTROL EXERCISES
< previous | Table of Contents | next > |