Lists act as containers, it can hold or store multiple data at the same time.
Lists are changeable and are ordered sequence of elements.
To know more about Python Lists you can visit the official Python Lists documentation.
To declare a list using [ ] brackets.
The elements inside the [ ] are the values
Python
# Creating an empty list called "list"lst = [ ]# Adding values called elements inside of the listlst = [ 1, 2, 3, 4, 5]# Printing the listprint(lst)
Output
[1, 2, 3, 4, 5]
Note: You should not use the variable name “list” as this is a reserved word
To access individual elements enter the variable name follows by a square bracket which has a number referring to which element you want.
Note lists start with index 0
For example:
lst = [ 1, 2, 3, 4, 5]print("The first output for lst[0] is: ",lst[0])print("The first output for lst[1] is: ",lst[1])
Output:
The first output for lst[0] is: 1The first output for lst[1] is: 2
Note if you get the error “IndexError: list index out of range”
You tried to get an element that is not in the list
You can get a set of elements by adding a start:stop in the square bracket.
For example we might want to take a sub list with the numbers 2,3,4
We are starting with index 1 and finishing at index
lst = [ 1, 2, 3, 4, 5]print("The output for lst[1:4] is: ",lst[1:4] )
output
The output for lst[1:3] is: [2, 3, 4]
Lists can also hold and contain strings
For Example:
lst = ["apple","banana","cherry"]print(lst)
This is used to add one element to the end of a list
list name .append(item)
Example:
lst = [ 1, 2, 3, 4, 5]lst.append(6)
print(lst)
[ 1, 2, 3, 4, 5,6]
This method allows you to add multiple items to the end of the list
list name.extend([items in square brackets separated by commas ])
lst = [ 1, 2, 3, 4, 5]lst.extend([6,7,8])print(lst)
[ 1, 2, 3, 4, 5,6,7,8]
This method is very similar to the extend method
This is used to concatenate lists together
list name = list name + list of new items
With this method you can add multiple items
lst = [ 1, 2, 3, 4, 5]
lst = lst + [6,7,8]
This can be shortened to:
lst = [ 1, 2, 3, 4, 5]lst += [6, 7,8]print(lst)
The insert method allows you to add an item at the specified index
list name.insert(The index(location and the item you want to add separated by a comma)
We will try to add 3 in its place in the list [1,2,4,5]
To do this we need to find the index we want to place our item
index 0 is 1
index 1 is 2
index 2 is 4
We want to place the item in index 2 so we will use lst.insert(2,3)
lst = [ 1, 2, 4, 5]lst.insert(2,3)print(lst)
[ 1, 2, 3, 4, 5]
If you specify a range using slice and assign another list or tuple, all items will be added.
lst[1:1] = [100, 200, 300]
[1, 100, 200, 300, 2, 4, 5]
You can also replace the original item. All items in the specified range are replaced.
lst = [ 1, 2, 3, 4, 5]lst[1:2] = [100, 200, 300]print(lst)
[1, 100, 200, 300, 3, 4, 5]
Credit
https://note.nkmk.me/en/python-list-append-extend-insert/
This method will clear all the items in a list
List name.clear()
lst = [ 1, 2, 3, 4, 5]lst.clear()print(lst)
[]
You can remove an item at a specified index
list name.pop(index)
In the lst [ 1, 2, 3, 4, 5] we want to remove the number 3
The number 3 is in index 2 so we will use lst.pop(2)
lst = [ 1, 2, 3, 4, 5]lst.pop(2)print(lst)
[ 1, 2, 4, 5]
You can remove the last item if you do not pass a parameter into pop
Example
lst = [ 1, 2, 3, 4, 5]lst.pop()print(lst)
[1, 2, 3, 4]
If you do not want to remove using the index you can remove by using the item name
We can use list name .remove(Item name)
lst = [ 1, 2, 3,-1, 4, 5]lst.remove(-1)print(lst)
[ 1,2,3, 4, 5]
Allows you to remove a sub list from a list
del lst[start of sublist index : end of sublist index]
If we had lst [1,200,300,400,2,3,4,5] and we wanted to remove items 200 300 and 400
We need to know the index of 200 and 400
The index of 200 is 1
The index of 400 is 3
Therefore the start is 1 and the end is 4, we can write del lst[1:4]
lst = [1,200,300,400,2,3,4,5]del lst[1:4]print(lst)
https://note.nkmk.me/en/python-list-clear-pop-remove-del/
To replace an item in a list you will need the index of the item you want to replace
list name[index] = new item
If we had lst=[ 1,2,3, 4, 5] and we wanted to replace the number 3 with the number 6
The index of 3 is 2 so
lst[2] = 6
lst=[ 1,2,3, 4, 5]lst[2] = 6
[ 1,2,6, 4, 5]
Credit: https://pythoner.name/en/exchange-list-items
To count how many items there are in total in your list use
len(list name)
lst = [ 1,2,3, 4, 5]print(len(lst))
5
If you wanted to find the index of the first occurrence of an item you can use list name .index()
If you wanted to find the index of 2 in lst [ 1,2,3, 4, 5] you can use lst.index(2)
lst = [ 1,2,3, 4, 5]print(lst.index(2))
1
list name.count(item)
if we wanted to count the number of 2’s in lst [10, 7, 7, 1, 3, 4, 4, 3, 8, 6, 2, 4, 7, 8, 9, 3, 1, 4, 9, 6, 9, 2, 2, 6, 4, 2, 2, 2, 9, 10, 9, 6] we can use lst.count(2)
lst = [10, 7, 7, 1, 3, 4, 4, 3, 8, 6, 2, 4, 7, 8, 9, 3, 1, 4, 9, 6, 9, 2, 2, 6, 4, 2, 2, 2, 9, 10, 9, 6]
print(lst.count(2))
6
If you want to reverse a list you can use list name.reverse()
lst = [10, 7, 7, 1, 3, 4]lst.reverse()print(lst)
[4, 3, 1, 7, 7, 10]
To sort a list in ascending order (from small to big) you can use the list name.sort() function
lst = [10, 7, 7, 1, 3, 4]lst.sort()print(lst)
[1, 3, 4, 7, 7, 10]
If you want to reverse a list in descending order you can pass the parameter reverse=True into sort
lst = [10, 7, 7, 1, 3, 4]lst.sort(reverse=True)print(lst)
[10, 7, 7, 4, 3, 1]
The .sort() method changes the original list. If we do not want to change the original list we can use sorted(list name)
originalList = [10, 7, 7, 1, 3, 4]newList = sorted(originalList)print("The original list is :", originalList)print("The new list is :",newList)
The original list is : [10, 7, 7, 1, 3, 4]The new list is : [1, 3, 4, 7, 7, 10]
The sorted method can also take in the parameter reverse=True
originalList = [10, 7, 7, 1, 3, 4]newList = sorted(originalList,reverse=True)print("The original list is :", originalList)print("The new list is :",newList)
The original list is : [10, 7, 7, 1, 3, 4]The new list is : [10, 7, 7, 4, 3, 1]