This method sorts the list in ascending order. The original list is updated
colors = ["voilet", "indigo", "blue", "green"]
colors.sort()
print(colors)
num = [4,2,5,3,6,1,2,1,2,8,9,7]
num.sort()
print(num)
['blue', 'green', 'indigo', 'voilet']
[1, 1, 2, 2, 2, 3, 4, 5, 6, 7, 8, 9]
What if you want to print the list in descending order?
We must give reverse=True as a parameter in the sort method.
colors = ["voilet", "indigo", "blue", "green"]
colors.sort(reverse=True)
print(colors)
num = [4,2,5,3,6,1,2,1,2,8,9,7]
num.sort(reverse=True)
print(num)
['voilet', 'indigo', 'green', 'blue']
[9, 8, 7, 6, 5, 4, 3, 2, 2, 2, 1, 1]
The reverse parameter is set to False
by default.
Do not mistake the reverse parameter with the reverse method.
This method reverses the order of the list.
colors = ["voilet", "indigo", "blue", "green"]
colors.reverse()
print(colors)
num = [4,2,5,3,6,1,2,1,2,8,9,7]
num.reverse()
print(num)
['green', 'blue', 'indigo', 'voilet']
[7, 9, 8, 2, 1, 2, 1, 6, 3, 5, 2, 4]
This method returns the index of the first occurrence( দুটো থাকলেও ১ম টাই বলবে ) of the list item.
colors = ["voilet", "green", "indigo", "blue", "green"]
print(colors.index("green"))
num = [4,2,5,3,6,1,2,1,3,2,8,9,7]
print(num.index(3))
Output:
1
3
Returns the count of the number of items with the given value.
colors = ["voilet", "green", "indigo", "blue", "green"]
print(colors.count("green"))
num = [4,2,5,3,6,1,2,1,3,2,8,9,7]
2
3
Returns copy of the list. This can be done to perform operations on the list without modifying the original list.
colors = ["voilet", "green", "indigo", "blue"]
newlist = colors.copy()
print(colors)
print(newlist)
['voilet', 'green', 'indigo', 'blue']
['voilet', 'green', 'indigo', 'blue']
This method appends items to the end of the existing list & changes the list.
colors = ["voilet", "indigo", "blue"]
colors.append("green")
print(colors)
['voilet', 'indigo', 'blue', 'green']
This method inserts an item at the given index. User has to specify index and the item to be inserted within the insert() method & changes the list.
colors = ["voilet", "indigo", "blue"]
# [0] [1] [2]
colors.insert(1, "green") #inserts item at index 1
# updated list: colors = ["voilet", "green", "indigo", "blue"]
# indexs [0] [1] [2] [3]
print(colors)
['voilet', 'green', 'indigo', 'blue']
This method adds an entire list or any other collection datatype (set, tuple, dictionary) to the existing list.
#add a list to a list
colors = ["voilet", "indigo", "blue"]
rainbow = ["green", "yellow", "orange", "red"]
colors.extend(rainbow)
print(colors)
['voilet', 'indigo', 'blue', 'green', 'yellow', 'orange', 'red']
countries = ["Spain", "Italy", "India", "England", "Germany"]
contries.pop(3)
print(contries)