Python Lists
About Python Lists
In Python, a list is a collection of items that are ordered.
Python list is mutable. You can change, add, and remove elements from it after it has been created.
A single list can store items of different data types.
Creating a list
You can create a list by enclosing a sequence of elements inside square brackets '[]'. Elements of list must be separated by comma (,).
In the following example, fruit_list is a variable of list and ["apple", "banana", "orange"] is the sequence of fruit names inside square brackets.
Example of creating list:
Output of the above example
['apple', 'banana', 'orange']
Creating list with elements of different data types
In Python, you can create a list with elements of different data types.
In the following example, mixed_list is a variable of list and ["apple", 5, True, 3.14] is the sequence of elements of different data types (string, integer, boolean, and float types) inside square brackets.
Example of creating list with elements of different data types:
Output of the above example
['apple', 5, True, 3.14]
Accessing elements of a list
In Python, you can access elements of a list using indexing. Indexing in Python is 0 based. If want to want to access first element, you need to use index 0.
The index is placed after the list variable without any space inside square brackets '[]'.
In the following example, num_list[0] is used to get the first element of the list where the num_list is the list and [1, 2, 3, 4 ,5 ,6] are the elements of the list. num_list[2] is used to get the third element of the list
Example of accessing elements of a list using indices:
Output of the above example
The first element of the list: 1
The third element of the list: 3
Accessing elements of a list using negative indices
In Python, you can also use negative indices to access elements from the end of the list.
Index '-1' refers to the last element, '-2' refers to the second last, and so on.
In the following example, num_list[-1] is used to get the last element of the list. num_list[-3] is used to get the third element from the end of the list.
Example of accessing elements of a list using negative indices:
Output of the above example
The last element of the list: 8
The third element from the end of the list: 6
Slicing a list to create a new list
In Python, you can create a new list from a subset of elements of an existing list.
This is done using slicing, which involves specifying a range of indices to select elements from the original list.
Please have a closer look at the following examples and try the code in our Python editor.
Example of slicing a list to create a new list:
Output of the above example
Elements from index 2 to 4: [3, 4, 5]
All elements upto index 4: [1, 2, 3, 4, 5]
All elements after index 3: [4, 5, 6, 7, 8]
Every second element of the list: [1, 3, 5, 7]
Every second element of the list reversely: [8, 6, 4, 2]
Explanation of the above example
-
In the first example,
num_list[2:5]refers that the selection will start from the index '2' and before the index '5' of the list (element from index 2 to 4 will be included in the list while element at index '5' will be excluded. The new list will be [3, 4, 5]. -
In the second example,
num_list[:5]refers that the selection will start from the beginning up to the index before index 5 (element from index 0 to 4 will be included in the list while element at index '5' will be excluded. The new list will be [1, 2, 3, 4, 5]. -
In the third example,
num_list[3:]refers that the selection will start after the index 3 to the end of the list (element from index 4 to the last index). The new list will be [4, 5, 6, 7, 8]. -
In the fourth example,
num_list[::2]refers that starting from the first every second element of the list will be selected. The new list will be [1, 3, 5, 7]. -
In the fifth example,
num_list[::-2]refers that starting from the last every second element of the list will be selected reversely. The new list will be [8, 6, 4, 2].
Python List Methods
The standard list methods available in Python are explained below:
-
append(): The append() method is used to add an element to the end of the list. In the following example,
x_list.append(5)is used to add '5' to the end of the x_list. The appended list will be [1, 2, 3, 4, 5]. -
insert(): The insert() method inserts an element at a specified index(position) in a list. In the following example,
y_list.insert(2,8)is used to insert '8' at the index 2 in the y_list. The inserted new list will be [6, 7, 8, 9]. -
remove(): The remove() method removes the first occurrence of a specified element from a list. In the following example,
y_list.remove(8)is used to remove '8' from the y_list. The new list will be [6, 7, 9]. -
pop(): The pop() method removes and returns the last element of a list by default. It can also remove and return the element at a specified index in a list. In the following example,
y_list.pop(1)is used to return element at index 1 as well as remove the element from the list. The new list will be [6, 9] and the returned element will be 7. -
clear(): The clear() method removes all elements of a list. In the following example,
y_list.clear()is used to remove all elements of y_list. -
sort(): The sort() method sorts the elements of the list. In the following example,
z_list.sort()is used to sort all elements of z_list. -
reverse(): The z_list.reverse() method reverses the elements of the list. In the following example,
z_list.reverse()is used to reverse all elements of z_list. -
count(): The z_list.count() method returns the number of occurrences of an element in the list. In the following example,
z_list.count(3)is used to return the number of occurrences of element '3' in the z_list.
Example of standard list methods:
Output of the above example
List after appending: [1, 2, 3, 4, 6]
List after inserting 8: [6, 7, 8, 9]
List after removing 8: [6, 7, 9]
List after popping: [6, 9] and returned element is: 7
List after clearing: []
List after sorting: [1, 2, 3, 3, 4, 5]
List after sorting: [1, 2, 3, 3, 4, 5]
List after reverse: [5, 4, 3, 3, 2, 1]
Occurrence of element 3 in the list: 2
Python Compiler For Live Practice
Practice the above Python code in the following Python Editor. Create more Python lists and try different list methods in the following Python code editor by clicking Execute button. Output will be displayed in Python Code Output section.
Python Editor