- A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements.
- Each element or value that is inside of a list is called an item.
- Just as strings are defined as characters between quotes, lists are defined by having values between square brackets [ ] and every value is separated by comma ” , “.
Syntax
friends = ['Hamza', 'Fatima', 'Aaliyan', 'Anzla', 'Ali', 'Zoya']
numbers = [1,2,3,4,5,6,7,8,9]
list_float = [ 2.43,98.67, 7.45, 45.65]
mix = ['Ahsan', 2, 4.56, 6.87, True, 'Maham', 56, false]
Common List Operations
- Access value
- Add a new value at the end/tail of the list
- Find the index of a value in the list
- Slicing the elements from the list
- Deleting and removing the elements from the List
- Popping elements from the list
Accessing List Elements
List elements are accessed by providing the identifier and index of elements inside square brackets. For example, in the following snippet arr is an identifier which points to a list.
arr = [1, True, "Pakistan", 3.5, 5, 9]
To access the element at Position 3 which is “Pakistan“, we write:
print (arr[2])
Please note that to access position 3, we have provided index 2, because the first element in the Python List always starts with a 0 (Zero) index.
Adding Values in the List
Python provides a function append() which adds the value at the end of the List. For example, arr points to a list in the following snippet
arr = [1, -3, 4.5, 32, 0.2, 4, 6]
To add a value 321 to the list we can do the following
arr.append(321)
Now if we check the elements in the list arr
we will get the following content
arr = [1, -3, 4.5, 32, 0.2, 4, 6, 321]
Searching/Finding a Value in List
Python provides a function index () which returns the index of the first occurrence of the value. For example, if arr points to a List
arr = [1, -3, 4.5, 32, 0.2, 4, 1]
To find a value 1 in the list we can do the following
arr.index(1)
This call will give the result 0 even if you execute this line many times, the reason is by default this function starts searching from index 0 and stops when it finds the first occurrence of the desired value.
Slicing the elements from the List
Python provides a very handy feature if you want to take a partial piece of a List. Note that the slicing operation copies elements from the original List. Hence this operation returns a new List which is a subset of the original List. Example syntax:
arr = [1, 4, 2, 78, 45, 23, 89]
print(arr[1: 4])
Outputs => [4, 2, 78]
print(arr[-2: -1])
Outputs => [23]
print(arr[1: : 2])
Outputs => [4, 78, 23]
Deleting and removing the value from the List
Although both are synonyms, Python and how they are used differ.
- There are cases when we know exactly the index of value we need to delete, in such cases we use the del keyword to delete the element. Example
arr = [1, 3, 2, 6, 4]
del arr[2]
Now if we check the contents of List arr we will get the following output
arr = [1, 3, 6, 4]
2. In case we know the value but do not know the index of that value in List, we call the remove function over the List identifier and pass the value to the function. Example
arr = [1, 3, 2, 6, 4]
arr.remove(2)
Now if we check the contents of List arr we will get the following output
arr = [1, 3, 6, 4]
Popping Elements from the List
Popping elements works in two ways
- We know the index of the value to remove
- We want to remove the element at the end / tail of List
Example:
arr = [1, 2, 4, 6, 7]
arr.pop() # will remove the value 7 from the end of List
arr.pop(2) # will remove the value 4 from the index 2 of List