Table of Contents
- What is a List?
- Declaring a List
- Accessing elements of a List
- Updating elements of a List
- Deleting elements of a List
- Slicing a List
- Frequently used List methods
What is a List?
List is a sequence of elements. A list is mutable, unlike a tuple in Python. By mutable, we mean that it is possible update a list after declaring one. The elements inside a list can be any Python object such a string, integer etc. Lists are surrounded by square brackets.
Declaring a List
Empty List is declared by empty square brackets.
A list with multiple elements is declared by having those elements separated by a comma.
Accessing elements of a List
We use square brackets along with the index which we want to access.
If we try to access some index that doesn’t exist, Python raises appropriate error. We can also access elements using negative indexing.
Index table indicating positive and negative index values.
Updating elements of a List
We saw that in Python, tuples and strings are immutable. However, lists are mutable. We can update the list by accessing its elements by index.
Deleting elements of a List
We can delete elements of a list by their index. For example:
We can also delete an entire list using
There a two methods on list which can be used to delete elements from the list,
list.remove(x)will remove the first occurrence of x in the list.
list.pop(idx)will remove and return the element at the position
- If we don’t provide
pop(), it will remove the last element in the list
Slicing a List
Using slicing we can extract out elements of any list. We have to provide the starting index and the ending index i.e.
It starts from
start and ends right before
end. It won’t print the element at the index
end. As in the example above, we have
n at the 4th index of list
l, however, it printed only till
start is out of the range of the list, then it prints empty list.
If you don’t provide
start, then it will print from
end is not provided, it prints till the last element of the list.
And if we don’t provide either of them? You guessed it right! It prints the whole list as is.
This is called deepcopy. It creates a copy of the list and returns another list. This is a very important concept and people tend to makes mistakes around it. Let’s take an example to understand this.
There are two terms, list assignment and list copying/deepcopying.
Here, we are confused, Why is only l2 affected and not l3. That is because, l2 points to the address of l1 in computer memory, but l3 is created as a whole new list and acquires specific memory in the computer that contains all the elements of l1. Visually, we can represent it in following way:
Frequently used List methods
Insert a new element at the end of the list.
Insert a new element at a specific position in the list.
Extend an existing list using another list.
Finding the index of any element in a list
Count the occurence of any element in a list
Reverse a list
Sort a list in place
Get a copy of sorted list
Some more common functions on list.
In Python, list is the most versatile data-type. You’ll find that list is used in almost every other complex data-structure in Python. Next, we will see yet another widely used data-type,
dict - Dictionaries in Python.
Note: This is a part of what I learned in an online Open Course Ware offered by MIT on edX. Its for my personal reference & also for those who would like to revisit the course.