Lists
In this Lecture, we'll learn everything about Python lists: creating lists, changing list elements, removing elements, and other list operations with the help of examples.
Python lists are one of the most versatile data types that allow us to work with multiple elements at once. For example,
Create Python Lists
In Python, a list is created by placing elements inside square brackets []
, separated by commas.
A list can have any number of items and they may be of different types (integer, float, string, etc.).
A list can also have another list as an item. This is called a nested list.
Access List Elements
There are various ways in which we can access the elements of a list.
List Index We can use the index operator [] to access an item in a list. In Python, indices start at 0. So, a list having 5 elements will have an index from 0 to 4.
Trying to access indexes other than these will raise an IndexError. The index must be an integer. We can't use float or other types, this will result in TypeError.
Nested lists are accessed using nested indexing.
Negative indexing
Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the second last item and so on.
List Slicing in Python
We can access a range of items in a list by using the slicing operator :
.
This simply means that Python will start at index 2 and go up to index 4 (but not including index 5).
This simply means that Python will start at index 2 and go up to the last index.
Note: When we slice lists, the start index is inclusive but the end index is exclusive. For example, my_list[2: 5] returns a list with elements at index 2, 3 and 4, but not 5.
Add/Change List Elements
Lists
are mutable, meaning their elements can be changed unlike string
or tuple
.
We can use the assignment operator =
to change an item or a range of items.
We can add one item to a list using the append()
method or add several items using the extend()
method.
We can also use +
operator to combine two lists. This is also called concatenation.
The *
operator repeats a list for the given number of times.
Furthermore, we can insert one item at a desired location by using the method insert()
or insert multiple items by squeezing it into an empty slice of a list.
Delete List Elements
We can delete one or more items from a list using the Python del statement
. It can even delete the list entirely.
We can use remove()
to remove the given item or pop()
to remove an item at the given index.
The pop()
method removes and returns the last item if the index is not provided. This helps us implement lists
as stacks
(first in, last out data structure).
And, if we have to empty the whole list, we can use the clear()
method.
Finally, we can also delete items in a list by assigning an empty list to a slice of elements.
List Methods
Python has many useful list methods that makes it really easy to work with lists. Here are some of the commonly used list methods.
Methods | Descriptions |
---|---|
| adds an element to the end of the list |
| adds all elements of a list to another list |
| inserts an item at the defined index |
| removes an item from the list |
| returns and removes an element at the given index |
| removes all items from the list |
| returns the index of the first matched item |
| returns the count of the number of items passed as an argument |
| sort items in a list in ascending order |
| reverse the order of items in the list |
| returns a shallow copy of the list |
NOTE: The above methods are not exaustive. There are many more methods available in the
list
module.
List Comprehension: Elegant way to create Lists
List comprehension is an elegant and concise way to create a new list from an existing list in Python.
A list comprehension consists of an expression followed by for statement inside square brackets.
Here is an example to make a list with each item being increasing power of 2.
This code is equivalent to:
A list comprehension can optionally contain more for or if statements. An optional if statement can filter out items for the new list. Here are some examples.
Other List Operations in Python
List Membership Test
We can test if an item exists in a list or not, using the keyword in.
Iterating Through a List
Using a for loop we can iterate through each item in a list.
We can use while loop to iterate through a list as well.
Resources:
Last updated