Lists are one of the most important data types in Python. They are flexible and easy to use. Lists are sequences of objects. Each element of the list can be of any type, including another list. Lists are ordered which means that each element can be referenced by an integer index.
Lists are dynamically sized and they are mutable. They must be declared in some manner before they can be used.
- Empty List
- Return a new list from a built-in function
L=list(range(12))This returns a list of integers 0,1,..11
As for all ordered types in Python, the indices start at 0. The upper bound in any range is the limit we do not reach.
To access a particular element by its index, we enclose the index value in square brackets.
Lists are mutable so individual elements can be changed.
myL=[1,2,3,5,6,7] myL=4 print(myL)
Sublists are often called slices.
Here the colon is again the range operator. Always remember that the upper bound is excluded, so this slice is elements 1 and 2, which are the second and third elements.
This extracts elements from the third to the last.
This extracts the elements from the beginning to the third element (index number 2).
A stride can also be specified
This extracts elements 1, 3, and 5.
- Initialize an empty list
- Initialize a list of known size (the size can be a variable but it must have a value when this statement is executed)
- Append an element to a list
- Extend a list with another list
Appending adds the argument as the new last element exactly as it appears. It takes any type. Extending requires a list as its argument. It concatenates that list at the end of the original one. It is is equivalent to
Insert an element
- This inserts
i. To add an item at the beginning of the list, use
- Delete an element by its index
- Delete the first occurrence of a particular element
itemmust match exactly or an error occurs.
- Remove and return an element
- The angle brackets indicate an optional argument and are not typed. If the argument is absent the last element is returned. If it is present that value is returned.
- Keep in mind that
popshortens the list.
Much more can be done with lists.
- Length (number of elements)
- Sum of values (if the same type and sum is defined)
- Maximum or minimum value of the items (if they are the same type and max and min are defined)
- Membership test (returns Boolean)
item in list
- Index of first time item occurs
- Number of times item occurs
- Sort a list (when possible) in place (overwrites the original)
- Return a sorted list to a new list
- Reverse the list in place (overwrites)
- There is no direct function to reverse and return into another list, so we use this handy trick
- In Python 3,
reversed(L)returns an iterator and not a list, but you may use the list constructor to convert it.
Print the length of the list. Change the fourth element to 11. Extend the list with L=[20,30,40] Print the index of the item 9 Remove that item from the list. Print the current length of the list. Sort the list and then reverse the sorted version.
A=[1,2,3,4] B=A print(A) B=9 print(A)
B is just an alias (a “nickname”) for A. If B changes so does A. This is true for all mutable types. Slicing notation creates a view that can make a copy if the entire list is included.
C=A[:] C=11 print(A) print(C)
An alternative is to explicitly use the list constructor function: