Introduction to List Method in Python
The List method in python can be defined as a data type used for storing a single or group of elements/ variables, and these data can be an integer, a character, or a float. The list method can be functioning with a number of operators like sort(), copy(), append(), clear(), reverse(), remove(), pop(), insert(), count(), extend() and index(). These functions are used for inserting, editing, deleting, rearranging, etc., the data present in the list of elements.
How do Lists work in Python?
Every value that is stored in the list has its address or known as index position. By index position, we can get any particular value from the list. All Index value starts from 0. Python reads the list from left to right in default. Square brackets surround every list. We can read the list element, but we cannot specify an index that does not exist in a list that will result in an error.
We can also access the element of the list from backward using the (-)negative index value. Negative starts from -1. If we specify -1, it will return the last element of the list and so on.
Lists are very helpful as it is used to store data of the same kind together. For example, all the students names or scholar numbers of students in a single list. Python list also supports nesting, so we can create a list inside another list.
names = ["Sanjay", "Ramesh", "Suresh", "Ravi"]
If we want to get any name, we have to specify the index position with the variable name.
names[0] = sanjay
names[2] = Suresh
If we want to reassign any value of the list, then we can simply assign a new value to the list variable with index position.
names[2] = "Akshay"
names = ["Sanjay", "Ramesh", "Akshay", "Ravi"]
Methods Available in Python List Function
Here are some important methods available in list python, which are as follows:
1. append()
This function is used to add value to the existing list. Just use the append keyword and add the name. The new value will be added at the end of the list. If we pass another list in the append function, it will add a list as the value.
names.append("John")
names = ["Sanjay", "Ramesh", "Akshay", "Ravi", "John"]
nums = [1,2,3]
names.append(nums)
["Sanjay", "Ramesh", "Akshay", "Ravi", "John",[1,2,3]]
2. sort()
This function is used to arrange the data alphabetically in ascending or descending order. By default, it does the sort in ascending order. If we need to do the sorting in descending order, we need to reverse the keyword.
names.sort()
["Akshay","John","Ramesh","Ravi","Sanjay"]
names.sort(reverse=True)
["Sanjay","Ravi","Ramesh","John","Akshay"]
3. copy()
If we want to make a copy of the list, we can assign it to a new variable, but in the case of Python, a copy might be created, but the problem is that if we change any value in the original list, then the value of the new list also changes. They are just sharing the reference of the original variable.
So to avoid this, we can use the copy() function.
new_names = names.copy()
print(new_names)
["Sanjay","Ravi","Ramesh","John","Akshay"]
names.append(1)
print(names)
["Sanjay","Ravi","Ramesh","John","Akshay",1]
print(new_names)
["Sanjay","Ravi","Ramesh","John","Akshay"]
4. clear()
This function is used to remove all the values from the list and return an empty list with a square bracket.
names.clear()
print(names)
[ ]
5. reverse()
This function is used to reverse the items of the list. It will read the element from right to left and modify the current list.
["Sanjay","Ravi","Ramesh","John","Akshay"]
names.reverse()
["Akshay","John","Ramesh","Ravi","Sanjay"]
6. remove()
If we want to remove any value from the list and don’t know the value’s index position, then we can use the remove() function. We have to pass the value which we want to remove from the list, and it will search that element from left to right, from index position 0, and whichever the matching element comes first will be removed.
["Sanjay","Ravi","Ramesh","John","Akshay"]
names.remove("Ravi")
print(names)
["Sanjay","Ramesh","John","Akshay"]
7. pop()
This function is also similar to the remove() function, but in this function, we can remove the value from the list based on an index, not a value. We must know the index position of the value in the list to remove it.
["Sanjay","Ravi","Ramesh","John","Akshay"]
names.pop[2]
print(names)
["Sanjay","Ravi","John","Akshay"]
8. insert()
This function is also used to insert the value into the list, but we can insert value at a specific index position using this function. The element that exists, in particular, will shift to the next index, which means all the elements after that index will increment by 1.
["Sanjay","Ravi","John","Akshay"]
names.insert(1,"David")
print(names)
["Sanjay","David","Ravi","John","Akshay"]
9. count()
This function is used to find which element exist and how many times in the list. It will count the total number based on which value is passed inside the function.
["Sanjay","Ravi","John","Akshay","John"]
names.count("David")
1
names.count("John")
2
10. extend()
This function is similar to the append() function only difference is that if we add a list in another list append() function will make it as a nested list, but in this function, if we add another list, it will add items on that list instead of making it nested.
names.append(nums)
print(names)
["Sanjay", "Ramesh", "Akshay", "Ravi", "John",1,2,3]
11. index()
This function is used to find the index of any element from the list. We need to pass the name of the value. Values are case-sensitive.
["Sanjay","Ravi","John","Akshay","John"]
names.index("Ravi")
1
Conclusion
The List is the most basic collection or sequence of data. The list is the data structure of the Python enclosed between square brackets. The list is mutable, so their values can be changed. Python provides many inbuilt functions for a list. Using those functions, we can perform various operations on the list.
Recommended Articles
This is a guide to the List Method in Python. Here we discuss the basic concept, working, and top 11 list method in python and its examples and code implementation. You may also look at the following articles to learn more-