24 Python List
24 Python List
We’ll learn everything about Python lists, how they are created, slicing of a list,
adding or removing elements from them and so on
# Empty list
My list = []
# List of integers
My list = [1, 2, 3]
# nested list
My list = ["mouse", [8, 4, 6], ['a']]
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 Index Error . The index must
be an integer. We can't use float or other types, this will result in Type Error .
# List indexing
# Output: p
print(my list[0])
# Output: o
print(my list[2])
# Output: e
print(my list[4])
# Nested List
n_list = ["Happy", [2, 0, 1, 5]]
# Nested indexing
print(n_list[0],[1])
print(my_list[4.0])(n_list[1][3])
Output
p
o
e
a
5
Traceback (most recent call last):
File "<string>", line 21, in <module>
TypeError: list indices must be integers or slices, not float
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.
print(my_list[-1])
print(my_list[-5])
When we run the above program, we will get the following output:
e
p
List indexing in
Python
Slicing can be best visualized by considering the index to be between the elements
as shown below. So if we want to access a range, we need two indices that will slice
that portion from the list.
Element Slicing
from a list in Python
print(odd)
print(odd)
Output
[1, 4, 6, 8]
[1, 3, 5, 7]
odd.append(7)
print(odd)
print(odd)
Output
[1, 3, 5, 7]
[1, 3, 5, 7, 9, 11, 13]
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.
print(["re"] * 3)
Output
[1, 3, 5, 9, 7, 5]
['re', 're', 're']
odd[2:2] = [5, 7]
print(odd)
Output
[1, 3, 9]
[1, 3, 5, 7, 9]
print(my_list)
print(my_list)
Output
We can use remove() method to remove the given item or pop() method 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).
We can also use the clear() method to empty a list.
my_list = ['p','r','o','b','l','e','m']
my_list.remove('p')
# Output: 'o'
print(my_list.pop(1))
# Output: 'm'
print(my_list.pop())
my_list.clear()
# Output: []
print(my_list)
Output
Finally, we can also delete items in a list by assigning an empty list to a slice of
elements.
>>> my_list = ['p','r','o','b','l','e','m']
>>> my_list[2:3] = []
>>> my_list
['p', 'r', 'b', 'l', 'e', 'm']
>>> my_list[2:5] = []
>>> my_list
['p', 'r', 'm']
They are accessed as list.method() . Some of the methods have already been used
above.
Python List Methods
# Output: 1
print(my_list.index(8))
# Output: 2
print(my_list.count(8))
my_list.sort()
# Output: [0, 1, 3, 4, 6, 8, 8]
print(my_list)
my_list.reverse()
# Output: [8, 8, 6, 4, 3, 1, 0]
print(my_list)
Output
1
2
[0, 1, 3, 4, 6, 8, 8]
[8, 8, 6, 4, 3, 1, 0]
Output
pow2 = []
for x in range(10):
pow2.append(2 ** x)
y# Output: True
print('p' in my_list)
# Output: False
print('a' in my_list)
# Output: True
print('c' not in my_list)
Output
True
False
True
Iterating Through a List
Output
I like apple
I like banana
I like mango