Lists
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.
Data Types in Lists
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.
Converting from another type
Growing a List: Append
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.
Growing a List: Plus (+)
In Python, lists can be added to each other using the plus symbol +
. This will result in a new list containing the same items in the same order with the first list’s items coming first.
An alternative way is to use .extend()
.
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.
Range
range()
is used to create a list of consecutive numbers.
By default, range()
takes a single input, and generates numbers starting at 0
and ending at the number before the input.
If we call range()
with two arguments, we can create a list that starts at a different number. For example, range(2, 9)
would generate numbers starting at 2
and ending at 8
(just before 9
):
If we use a third argument, we can create a list that “skips” numbers. For example, range(2, 9, 2)
will give us a list where each number is 2
greater than the previous number:
Determining List Length withlen()
len()
The Python len()
function can be used to determine the number of items found in the list it accepts as an argument.
Selecting List Elements
In Python, we call the order of an element in a list its index. Python lists are zero-indexed. This means that the first element in a list has index 0
, rather than 1
.
Selecting one element from the beginning
We can select a single element from a list by using square brackets ([]
) and the index of the list item.
Selecting one element from the end
Negative indices for lists in Python can be used to reference elements in relation to the end of a list.
Slicing Lists
A slice, or sub-list of Python list elements can be selected from a list using a colon-separated starting and ending point.
The syntax pattern is myList[START_NUMBER:END_NUMBER]
. The slice will include the START_NUMBER
index, and everything until but excluding the END_NUMBER
item.
When slicing a list, a new list is returned, so if the slice is saved and then altered, the original list remains the same.
When selecting a range of list items, if the first item to be selected is at index 0
, no index needs to be specified before the :
. Similarly, if the last item selected is the last item in the list, no index needs to be specified after the :
Slicing skipped elements
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,
my_list[-1]
.To select the last three elements,
my_list[-3:]
.To select everything except the last two elements,
my_list[:-2]
.
Counting elements in a list
The .count()
Python list method searches a list for whatever search term it receives as an argument, then returns the number of matching entries found.
Sorting
The .sort()
Python list method will sort the contents of whatever list it is called on. Numerical lists will be sorted in ascending order, and lists of Strings will be sorted into alphabetical order. It modifies the original list, and has no return value.
The Python sorted()
function accepts a list as an argument, and will return a new, sorted list containing the same elements as the original. Numerical lists will be sorted in ascending order, and lists of Strings will be sorted into alphabetical order. It does not modify the original, unsorted list.
Aggregating Iterables Usingzip()
zip()
In Python, data types that can be iterated (called iterables) can be used with the zip()
function to aggregate data based on the iterables passed in.
zip
takes two (or more) lists as inputs and returns an object that contains a list of pairs. Each pair contains one element from each of the inputs.
As shown in the example, zip()
is aggregating the data between the owners’ names and the dogs’ names to match the owner to their dogs. zip()
returns an iterator containing the data based on what the user passes through and can be printed to visually represent the aggregated data. Empty iterables passed in will result in an empty iterator.
Checking membership
Shallow copy
Replacing an existing element
Replacing multiple existing elements
Insert a element into a list at specific index
Removing elements
Removing an element from the end
Removing an element from any index
Removing an element based on its value (rather than its position)
Removing a slice
Reversing
Iterating over the elements
Iterating over the sorted elements
Find max/min value in a list
Last updated
Was this helpful?