Python List
A List is the collection of elements. The list is the most frequently used data types in python.
How to create a list?
A list is created by placing elements inside square brackets [] these elements are separated by commas. A List can contain similar or different data types.
For example
# A list of integers
_list = [1, 2, 3]
print(_list)
# list with heterogeneous data types
_list = [0, "Hi", 3.14159]
print(_list)
# empty list
_list = []
print(_list)
Output
[1, 2, 3]
[0, 'Hi', 3.14159]
[]
A list can contain another list as an element, It is called a nested list.
# Nested List
_list = [0, [1,2,3], 3.14159]
How to create a list using a sequence?
The list() function is used to create the list from the sequence for example
# creating a list using the tuple
_list = list( ( 1,2,3,4))
print(_list)
# Creating a list using the string
_list = list( 'Python')
print(_list)
# creating list using another list
_list = list( [ 1,2,3,4])
print(_list)
Output
[1, 2, 3, 4]
['P', 'y', 't', 'h', 'o', 'n']
[1, 2, 3, 4]
How to access element(s) from a list?
There are two ways to access the element(s) from the list
- Using Index operator
- Index
- Negative Index
- Using Slicing
List Index
The index operator [] is used to access an element from a list.
The index or indices start from 0, the first element of the list is _list[0]
A list with 6 elements will have indexes from 0 to 5 for example
Index |
0 |
1 |
2 |
3 |
4 |
5 |
Element |
'P' |
'y' |
't' |
'h' |
'o' |
'n' |
Note:
The valid index range for a list is 0 to len(list) - 1, 0 to length of the list - 1
Accessing indexes other than these ( 0 to length of list -1) will raise an IndexError.
The index must be an integer. Using other than an integer as an index will result into TypeError
Nested lists are accessed using nested indexing.
_list = ['P', 'y', 't', 'h', 'o', 'n']
print(_list[0])
print(_list[5])
print(_list[6])
print(_list['a'])
Output
P
n
Traceback (most recent call last):
File "test.py", line 117, in
print(_list[6])
IndexError: list index out of range
Traceback (most recent call last):
File "test.py", line 120, in
print(_list['a'])
TypeError: list indices must be integers or slices, not str
Negative indexing
Python allows negative indexing to access elements from sequences. The index -1 refers to the last element of the list.
Index |
0 |
1 |
2 |
3 |
4 |
5 |
Element |
'P' |
'y' |
't' |
'h' |
'o' |
'n' |
Negative Index |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Example using Negative Index
_list = ['P', 'y', 't', 'h', 'o', 'n']
print(_list[-1])
print(_list[-6])
Output
n
P
Slicing
Slicing is used to select a range of elements from the list using colon ( : ) operator for example
Example: 1
_list[2:5]
Index |
0 |
1 |
2 |
3 |
4 |
5 |
Element |
'P' |
'y' |
't' |
'h' |
'o' |
'n' |
Negative Index |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Output
tho
Example: 2
_list[1:] # slicing list from index 1 to end of the list
Index |
0 |
1 |
2 |
3 |
4 |
5 |
Element |
'P' |
'y' |
't' |
'h' |
'o' |
'n' |
Negative Index |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Output
ython
Example: 3
_list[-1: -5] # slicing list from index 1 to end of the list
Index |
0 |
1 |
2 |
3 |
4 |
5 |
Element |
'P' |
'y' |
't' |
'h' |
'o' |
'n' |
Negative Index |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Output
ython
Example: 4
_list[4:] # slicing list from index 4 to the end of the list
Index |
0 |
1 |
2 |
3 |
4 |
5 |
Element |
'P' |
'y' |
't' |
'h' |
'o' |
'n' |
Negative Index |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Output
on
Example: 5
_list[:-4] # slicing list from index -6 to -5
Index |
0 |
1 |
2 |
3 |
4 |
5 |
Element |
'P' |
'y' |
't' |
'h' |
'o' |
'n' |
Negative Index |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Output
Py
Example: 6
_list[:] # all the elements from the list
Index |
0 |
1 |
2 |
3 |
4 |
5 |
Element |
'P' |
'y' |
't' |
'h' |
'o' |
'n' |
Negative Index |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Output
Python
Example: 7 using step parameter with slicing
Output
Python
_list = [1,2,3,4,5,6,7,8]
print(_list[0:7:2])
Index |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
Element |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
Negative Index |
-8 |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Output
[1,3,5,7]
Example 8
_list = [1,2,3,4,5,6,7,8]
print(_list[1::2])
Index |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
Element |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
Negative Index |
-8 |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Output
[2,4,6,8]
Example 9
_list = [1,2,3,4,5,6,7,8]
print(_list[::3])
Index |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
Element |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
Negative Index |
-8 |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Output
[1,4,7]
How to modify the element(s) from a list?
There are two ways to modify the element(s) from the list
- Using Index operator
- Using Slicing
Example: 1 Using index operator
_list = ['p', 'y', 't', 'h', 'o', 'n']
_list[0]= 'P'
print(_list)
Output
['P', 'y', 't', 'h', 'o', 'n']
Example: 2 Using index operator
_list = ['p', 'y', 't', 'h', 'o', 'n']
_list[-1 ]= 'P'
print(_list)
Output
['p', 'y', 't', 'h', 'o', 'N']
Slicing can be used to modify elements from the list two indices that will slice that portion of the list.
Example:1
_list = [1,2,5,7]
# Modify index 2 and 3 with values 3 and 4
_list[2:4] = [3,4]
print(_list)
Output
[1,2,3,4]
How to add element(s) to a list?
There are multiple ways to add element(s) to the list, enlist below
Using slicing
Using the insert() function
Using extend function
Using concatenation +
Using append() function
Example:1 Add elements using slicing operator
_list = [1,2,4,7,9]
_list[1:3]= [3,5]
print(_list)
Output
[1,3,5,7,9]
Example: 2 Add elements using insert() function
_list = [1,7,9]
_list.insert(1,3)
_list.insert(2,5)
print(_list)
Output
[1,3,5,7,9]
Example: 3 Add elements or list using extend() function
# The extend() function add another list to a list
_list = [2,4,6]
_list.extend([8,10])
print(_list)
Output
[2,4,6,8,10]
Example: 4 Concatenation lists
# The + Operator to concatenate two lists
_list = [2,4,6]
_list + [8,10]
print(_list)
Output
[2,4,6,8,10]
Example: 5 Add element using append() function
# The append() function adds an element at the end of the list
_list = [2,4,6]
_list.append(8)
_list.append(10)
print(_list)
Output
[2,4,6,8,10]
How to delete or remove elements from a list?
We can delete one or more element(s) using the following ways
Using del and index
Using slicing
Using remove () function
Using pop() function
Using clear() function
Example: 1 Delete element using del and index
_list = ['P', 'y', 't', 'h', 'o', 'n']
del _list[0]
print(_list)
Output
['y', 't', 'h', 'o', 'n']
Example: 2 Delete element(s) slicing
_list = ['P', 'y', 't', 'h', 'o', 'n']
_list[0:4] = []
print(_list)
Output
['o', 'n']
Example: 3 Delete element using remove() function
_list = ['P', 'y', 't', 'h', 'o', 'n']
#remove an element from a list
print(_list.remove('n'))
print(_list)
Output
['o', 'n']
Note: removing an not existence element a ‘ ValueError: list.remove(x): x not in list ‘ is raised.
Example: 4 Delete element using pop() function
_list = ['P', 'y', 't', 'h', 'o', 'n']
#remove an element from the end of the list
print(_list.pop'))
print(_list)
Output
n
['P', 'y','t', 'h','o']
Example: 5 Delete element using pop(index) function and an index
_list = ['P', 'y', 't', 'h', 'o', 'n']
#remove an element from a list at index 0
print(_list.pop(0))
print(_list)
Output
'P'
['y','t','h','o', 'n']
Example: 6 Delete all the elements from a list
_list = ['P', 'y', 't', 'h', 'o', 'n']
#remove all the elements from a list
_list.clear()
print(_list)
Output
[]
Example: 7 Delete a list
_list = ['P', 'y', 't', 'h', 'o', 'n']
#delete the list completely
del _list
print (_list)
Output
NameError: name '_list' is not defined
List Membership Test
The 'in ' and 'not in' keyword is used to find the existence of an element in the list.
_list = [1,2,3,4,5,6,7]
print( 4 in _list)
print( 10 in _list)
print( 4 not in _list)
print( 10 not in _list)
Output
True
False
False
True
Iterating through a List
You can iterate the list using 'for' loop, since the list is an iterable object
Example: 1 Iterating list with for loop
_list = ['P', 'y', 't', 'h', 'o', 'n']
for element in _list:
print(element)
Output
P
n
P
y
t
h
o
n
Example: 2 Iterating a list with the while loop
counter=0
length = len(_list)
while(counter < length):
print(_list(counter))
Output
P
n
P
y
t
h
o
n