Day 23 - List Methods

List Methods
1. list.sort()
This method sorts the list in ascending order. The original list is updated
Example 1:
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)
Output:
['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.
Example:
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)
Output:
['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.
Note: Do not mistake the reverse parameter with the reverse method.
2. reverse()
This method reverses the order of the list.
Example:
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)
Output:
['green', 'blue', 'indigo', 'voilet']
[7, 9, 8, 2, 1, 2, 1, 6, 3, 5, 2, 4]
3. index()
This method returns the index of the first occurrence of the list item.
Example:
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
4. count()
Returns the count of the number of items with the given value.
Example:
colors = ["voilet", "green", "indigo", "blue", "green"]
print(colors.count("green"))
num = [4,2,5,3,6,1,2,1,3,2,8,9,7]
Output:
2
3
5. copy()
Returns copy of the list. This can be done to perform operations on the list without modifying the original list.
Example:
colors = ["voilet", "green", "indigo", "blue"]
newlist = colors.copy()
print(colors)
print(newlist)
Output:
['voilet', 'green', 'indigo', 'blue']
['voilet', 'green', 'indigo', 'blue']
6. append()
This method appends items to the end of the existing list.
Example:
colors = ["voilet", "indigo", "blue"]
colors.append("green")
print(colors)
Output:
['voilet', 'indigo', 'blue', 'green']
7. insert()
This method inserts an item at the given index. User has to specify index and the item to be inserted within the insert() method.
Example:
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)
Output:
['voilet', 'green', 'indigo', 'blue']
8. extend()
This method adds an entire list or any other collection datatype (set, tuple, dictionary) to the existing list.
Example 1:
#add a list to a list
colors = ["voilet", "indigo", "blue"]
rainbow = ["green", "yellow", "orange", "red"]
colors.extend(rainbow)
print(colors)
Output:
['voilet', 'indigo', 'blue', 'green', 'yellow', 'orange', 'red']
Concatenating two lists:
You can simply concatenate two lists to join two lists.
Example:
colors = ["voilet", "indigo", "blue", "green"]
colors2 = ["yellow", "orange", "red"]
print(colors + colors2)
Output:
['voilet', 'indigo', 'blue', 'green', 'yellow', 'orange', 'red']
Practice
l = [11, 45, 1, 2, 4, 6, 1, 1]
print(l)
# l.append(7) Output: [11, 45, 1, 2, 4, 6, 1, 1, 7]
# l.sort(reverse=True) Output: [45, 11, 7, 6, 4, 2, 1, 1, 1]
# l.reverse() Output: [1, 1, 1, 2, 4, 6, 7, 11, 45]
# print(l.index(1)) Output: 0
# print(l.count(1)) Output: 3
# m = l.copy() Output: m[0] = 0
# l.insert(1, 899) Output: [1, 899, 1, 1, 2, 4, 6, 7, 11, 45]
m = [900, 1000, 1100]
k = l + m
# print(k)
# Output: [1, 899, 1, 1, 2, 4, 6, 7, 11, 45, 0, 1, 1, 2, 4, 6, 7, 11, 45]
# l.extend(m)
# Output: [1, 899, 1, 1, 2, 4, 6, 7, 11, 45, 0, 1, 1, 2, 4, 6, 7, 11, 45]
print(l)
Output:
[11, 45, 1, 2, 4, 6, 1, 1]




