Method | Description |
---|---|
Used to add an element at the end of the list | |
Used to add an element at the specified position | |
Used to add the elements of a list (or any iterable), to the end of the current list | |
Used to find the index of the first element with the specified value | |
Used to copy a list | |
Used to find the number of elements with the specified value | |
Used to removes all the elements from the list | |
Used to remove the elements form a index value | |
Used to remove specified element from the list | |
Used to remove the element at the specified position | |
Used to sort(in assending order) the list | |
Used to reverse all the elements in the list | |
Used to find the length of the list |
()
Method
Elements can be added to the List by using the built-in append() function.
Only one element at a time can be added to the list by using the append() method, for the addition of multiple elements with the append() method, loops are used.
Tuples can also be added to the list with the use of the append method because tuples are immutable.
Unlike Sets, Lists can also be added to the existing list with the use of the append() method.
Example:
a = [1 ,12 ,32 ,10 ,4 ,1.4 ]
a.append (34 )
print (a)
a.append ('hello' )
print (a)
b = ["world" ,"of" ,"python" ]
a.append (b)
print (a)
()
Method
append() method only works for the addition of elements at the end of the List, for the addition of elements at the desired position, insert() method is used.
Unlike append() which takes only one argument, the insert() method requires two arguments(position, value).
Example:
a = [222 ,298 ,271 ,234 ]
a.insert (2,"hello world" )
print (a)
a.insert (0,1.23 )
print (a)
()
Method
Other than append() and insert() methods, there’s one more method for the Addition of elements, extend(), this method is used to add multiple elements at the same time at the end of the list.
Example:
letters = ["a" ,"b" ,"c" ,"d" ]
numbers = [97 ,98 ,99 ,100 ]
letters.extend (numbers)
print (letters)
()
Method
Python index() is an inbuilt function in Python, which searches for a given element from the start of the list and returns the index of the first occurrence.
when we pass two arguments in the index function, the first argument is treated as the element to be searched and the second argument is the index from where the searching begins.
Example:
listt = ["a1" ,"a2" ,"a3" ,"a4" ,"a5" ]
num = listt.index ("a4" )
print (num)
numm = listt.index ("a1" )
print (numm)
()
Method
Python List Copy() methods return a shallow copy of a list(returns a new list without modifying the original lists).
Sometimes, there is a need to reuse any object, hence copy methods are always of great utility. Python in its language offers a number of ways to achieve this.
This particular article aims at demonstrating the copy method present in the list.
Since the list is widely used hence, its copy is also necessary.
The copy() method doesn’t take any parameters.
Example:
old_list = [1 ,10 ,100 ,1000 ,10000 ,100000 ,1000000 ,10000000 ]
copy_list = old_list.copy ()
print (copy_list)
()
Method
List count() method returns the count of how many times a given object occurs in a List.
Python list count() method raises TypeError when more than 1 parameter is passed.
Example:
list1 = [1 ,2 ,1 ,5 ,1 ,2 ,1 ,3 ]
num1 = list1.count (1 )
num2 = list1.count (5 )
num3 = list1.count (8 )
print (num1) ## 4
print (num2) ## 1
print (num3) ## 0
()
Method
This function is used to erase all the elements of list. After this operation, list becomes empty.
Example:
l = [1 ,2 ,1 ,5 ,1 ,2 ,1 ,3 ]
print (l)
l.clear ()
print (l)
()
Method
This method deletes all the elements in range starting from index ‘a’ till ‘b’ mentioned in arguments.--list[a:b]
Example:
listname = [1 ,2 ,1 ,5 ,1 ,2 ,1 ,3 ]
print (listname)
del listname[2:5]
print (listname)
()
Method
Elements can be removed from the List by using the built-in remove() function but an Error arises if the element doesn’t exist in the list.
remove() method only removes one element at a time, to remove a range of elements, the iterator is used. The remove() method removes the specified item.
Example:
list1 = [1 ,2 ,1 ,5 ,1 ,2 ,1 ,3 ]
list1.remove (1)
print (list1)
list1.remove (5)
print (list1)
list1.remove (1)
print (list1)
()
Method
pop() function can also be used to remove and return an element from the list, but by default it removes only the last element of the list, to remove an element from a specific position of the List, the index of the element is passed as an argument to the pop() method.
Example:
list1 = [1 ,2 ,1 ,5 ,1 ,2 ,1 ,3 ]
list1.pop (1)
print (list1)
list1.pop (3)
print (list1)
list1.pop (5)
print (list1)
()
Method
This function sorts the list in increasing order.
Example:
list1 = [1 ,2 ,1 ,5 ,1 ,2 ,1 ,3 ]
list1.sort ()
print (list1)
list1.sort (reverse = True )
print (list1)
list1.sort (reverse = False )
print (list1)
()
Method
A list can be reversed by using the reverse() method in Python.
Example:
list1 = [1 ,2 ,1 ,5 ,1 ,2 ,1 ,3 ]
list1.reverse ()
print (list1)
()
Method
The len() method offers the most used and easy way to find the length of any list. This is the most conventional technique adopted by all programmers today.
Example:
list1 = [1 ,2 ,1 ,5 ,1 ,2 ,1 ,3 ]
length = len (list1)
print (length)