In Python, lists are ordered collections of items that allow for easy use of a set of data.
List values are placed in between square brackets
[ ], separated by commas. It is good practice to put a space between the comma and the next value. The values in a list do not need to be unique (the same value can be repeated).
Empty lists do not contain any values within the square brackets.
primes = [2, 3, 5, 7, 11] print(primes) empty_list = 
Adding Lists Together
In Python, lists can be added to each other using the plus symbol
+. As shown in the code block, this will result in a new list containing the same items in the same order with the first list’s items coming first.
Note: This will not work for adding one item at a time (use
.append() method). In order to add one item, create a new list with a single value and then use the plus symbol to add the list.
items = ['cake', 'cookie', 'bread'] total_items = items + ['biscuit', 'tart'] print(total_items) # Result: ['cake', 'cookie', 'bread', 'biscuit', 'tart']
Python Lists: Data Types
In Python, lists are a versatile data type that can contain multiple different data types within the same square brackets. The possible data types within a list include numbers, strings, other objects, and even other lists.
numbers = [1, 2, 3, 4, 10] names = ['Jenny', 'Sam', 'Alexis'] mixed = ['Jenny', 1, 2] list_of_lists = [['a', 1], ['b', 2]]
In Python, you can add values to the end of a list using the
.append() method. This will place the object passed in as a new element at the very end of the list. Printing the list afterwards will visually show the appended value. This
.append() method is not to be confused with returning an entirely new list with the passed object.
orders = ['daisies', 'periwinkle'] orders.append('tulips') print(orders) # Result: ['daisies', 'periwinkle', 'tulips']
In Python, list index begins at zero and ends at the length of the list minus one. For example, in this list,
'Andy' is found at index
names = ['Roger', 'Rafael', 'Andy', 'Novak']
Python list elements are ordered by index, a number referring to their placement in the list. List indices start at 0 and increment by one.
To access a list element by index, square bracket notation is used:
berries = ["blueberry", "cranberry", "raspberry"] berries # "blueberry" berries # "raspberry"
Negative List Indices
Negative indices for lists in Python can be used to reference elements in relation to the end of a list. This can be used to access single list elements or as part of defining a list range. For instance:
- To select the last element,
- To select the last three elements,
- To select everything except the last two elements,
soups = ['minestrone', 'lentil', 'pho', 'laksa'] soups[-1] # 'laksa' soups[-3:] # 'lentil', 'pho', 'laksa' soups[:-2] # 'minestrone', 'lentil'
Modifying 2D Lists
In order to modify elements in a 2D list, an index for the sublist and the index for the element of the sublist need to be provided. The format for this is
list[sublist_index][element_in_sublist_index] = new_value.
# A 2D list of names and hobbies class_name_hobbies = [["Jenny", "Breakdancing"], ["Alexus", "Photography"], ["Grace", "Soccer"]] # The sublist of Jenny is at index 0. The hobby is at index 1 of the sublist. class_name_hobbies = "Meditation" print(class_name_hobbies) # Output # [["Jenny", "Meditation"], ["Alexus", "Photography"], ["Grace", "Soccer"]]
Accessing 2D Lists
In order to access elements in a 2D list, an index for the sublist and the index for the element of the sublist both need to be provided. The format for this is
# 2D list of people's heights heights = [["Noelle", 61], ["Ali", 70], ["Sam", 67]] # Access the sublist at index 0, and then access the 1st index of that sublist. noelles_height = heights print(noelles_height) # Output # 61
.remove() method in Python is used to remove an element from a list by passing in the value of the element to be removed as an argument. In the case where two or more elements in the list have the same value, the first occurrence of the element is removed.
# Create a list shopping_line = ["Cole", "Kip", "Chris", "Sylvana", "Chris"] # Removes the first occurance of "Chris" shopping_line.remove("Chris") print(shopping_line) # Output # ["Cole", "Kip", "Sylvana", "Chris"]