Introduction
Welcome to my 23rd blog post. Today I learned about 'Lists Methods' in python. I learned about sort(), reverse(), index(), count(), copy(), append(), insert(), copy(), extend() and concatenate 2 lists. These are some of the commonly used list methods among many in python. Now let's dive deep into the details of list methods for a better understanding
So let's get started......
list.sort()
This method sorts the list in ascending order. Here the original list gets updated.
Example 1 -
l = [11, 45, 1, 2, 4, 6, 1, 1]
print(l)
l.sort() #sorts list in ascending order
print(l)
Output -
[11, 45, 1, 2, 4, 6, 1, 1]
[1, 1, 1, 2, 4, 6, 11, 45]
Example 2 -
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]
Printing the list in descending order, we make use of (reverse = True) as a parameter in the sort method. The reverse parameter is set to False by default.
Note: Do not mistake the reverse parameter with the reverse method.
Example 1 -
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]
Example 2 -
l = [11, 45, 1, 2, 4, 6, 1, 1]
print(l)
l.sort() #sorts list in ascending order
print(l)
l.sort(reverse=True) #sorts list in descending order
print(l)
Output -
[11, 45, 1, 2, 4, 6, 1, 1]
[1, 1, 1, 2, 4, 6, 11, 45]
[45, 11, 6, 4, 2, 1, 1, 1]
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]
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))
1
3
count()
This method returns the count of the number of items with the given value
Example 1 -
l = [11, 45, 1, 2, 4, 6, 1, 1]
print(l)
print(l.index(1))
print(l.count(1))
[11, 45, 1, 2, 4, 6, 1, 1]
2
3
Example 2 -
colors = ["voilet", "green", "indigo", "blue", "green"]
print(colors.count("green"))
num = [4,2,5,3,6,1,2,1,3,2,8,9,7]
print(num.count(2))
Output -
2
3
copy()
Returns copy of the list. This can be done to perform operations on the list without modifying the original list.
Example 1 -
colors = ["voilet", "green", "indigo", "blue"]
newlist = colors.copy()
print(colors)
print(newlist)
['voilet', 'green', 'indigo', 'blue']
['voilet', 'green', 'indigo', 'blue']
Example 2 -
l = [11, 45, 1, 2, 4, 6, 1, 1]
print(l)
m = l #here m is the reference of l, hence changes made in m are reflected in l
m[0] = 0
print(l)
m = l.copy() #here a copy of l is made, here l is same as OG list
Output -
[11, 45, 1, 2, 4, 6, 1, 1]
[0, 45, 1, 2, 4, 6, 1, 1] #note here l is changed as we canged m
append()
This method appends items to the end of the existing list.
Example -
colors = ["voilet", "indigo", "blue"]
colors.append("green")
print(colors)
['voilet', 'indigo', 'blue', 'green']
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 1 -
l = [11, 45, 1, 2, 4, 6, 1, 1]
print(l)
l.insert(1, 899) #here 899 is inserted at index 1
print(l)
[11, 45, 1, 2, 4, 6, 1, 1]
[11, 899, 45, 1, 2, 4, 6, 1, 1]
Example 2 -
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']
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)
['voilet', 'indigo', 'blue', 'green', 'yellow', 'orange', 'red']
Example 2 -
l = [11, 45, 1, 2, 4, 6, 1, 1]
print(l)
m = [900, 1000, 1100]
l.extend(m)
print(l)
[11, 45, 1, 2, 4, 6, 1, 1]
[11, 45, 1, 2, 4, 6, 1, 1, 900, 1000, 1100]
Concatenating 2 lists:
You can simply concatenate two lists to join two lists.
Example -
colors = ["voilet", "indigo", "blue", "green"]
colors2 = ["yellow", "orange", "red"]
print(colors + colors2)
['voilet', 'indigo', 'blue', 'green', 'yellow', 'orange', 'red']
Resources Used
You can watch the video of Day#23 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. On day #23 we learned a lot about the list methods i.e commonly used list methods among many. We also wrote a bunch of code while practically solving the methods.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of python coding. This is it for Day #23
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.