List methods
Python List append() Method
Python append() method adds an item to the end of the list. It appends an element by modifying the list. The method does not return itself. The item can also be a list or dictionary which makes a nested list. Method is described below.
Signature
append(x)
Parameters
x: It can be a number, list, string, dictionary etc.
Return
It does not return any value rather modifies the list.
Python List append() Method Example 1
First, let's see a simple example which appends elements to the list.
# Python list append() Method
# Creating a list
list = ['1','2','3']
for l in list: # Iterating list
print(l)
# Appending element to the list
list.append(4)
print("List after appending element : ",list) # Displaying list
Output:
1
2
3
List after appending element : ['1', '2', '3', 4]
Python List append() Method Example 2
Appending a list to the list is also possible which will create a list inside the list. See the example below.
# Python list append() Method
# Creating a list
list = ['1','2','3']
for l in list: # Iterating list
print(l)
# Appending a list to the list
list2 = ['4','5','6','7']
list.append(list2)
print("List after appending element : ", list) # Displaying list
Output:
1
2
3
List after appending element : ['1', '2', '3', ['4', '5', '6', '7']]
Python List clear() Method
Python clear() method removes all the elements from the list. It clear the list completely and returns nothing. It does not require any parameter and returns no exception if the list is already empty. The syntax and examples are described below.
Signature
clear()
Parameters
No parameter
Return
It returns None.
Python List clear() Method Example 1
Let's see a simple example in which clear() method is used to clear a list. The clear() method clears all the elements from the list.
# Python list clear() Method
# Creating a list
list = ['1','2','3']
for l in list: # Iterating list
print(l)
list.clear()
print("After clearing:")
for l in list: # Iterating list
print(l)
Output:
1
2
3
After clearing:
Python List clear() Method Example 2
If the list is already empty, the method returns nothing. See the example below.
# Python list clear() Method
# Creating a list
list = []
for l in list: # Iterating list
print(l)
list.clear()
print("After clearing:")
for l in list: # Iterating list
print(l)
Output:
After clearing:
Python List copy() Method
Python copy() method copies the list and returns the copied list. It does not take any parameter and returns a list. The method signature and examples are given below.
Signature
copy()
Parameters
No parameter
Return
It returns a copy of the list.
Let's see some examples of copy() method to understand it's functionality.
Python List copy() Method Example 1
A Simple example which copy a list ot another and makes an new list. See the example below.
# Python list copy() Method
# Creating a list
evenlist = [6,8,2,4] # int list
copylist = []
# Calling Method
copylist = evenlist.copy()
# Displaying result
print("Original list:",evenlist)
print("Copy list:",copylist)
Output:
Original list: [6, 8, 2, 4]
Copy list: [6, 8, 2, 4]
Python List copy() Method Example 2
Here, we are using list slicing concept to create a copy of the list. See the example below.
# Python list copy() Method
# Creating a list
evenlist = [6,8,2,4] # int list
copylist = []
# Calling Method
copylist = evenlist[:] # Copy all the elements
# Displaying result
print("Original list:",evenlist)
print("Copy list:",copylist)
Output:
Original list: [6, 8, 2, 4]
Copy list: [6, 8, 2, 4]
next →← prev
Python List count() Method
Python count() method returns the number of times element appears in the list. If the element is not present in the list, it returns 0. The examples and syntax is described below.
Signature
count(x)
Parameters
x: element to be counted.
Return
It returns number of times x occurred in the list.
Let's see some examples of count() method to understand it's functionality.
Python List count() Method Example 1
It is a simple example to understand how a count method works.
# Python list count() Method
# Creating a list
apple = ['a','p','p','l','e']
# Method calling
count = apple.count('p')
# Displaying result
print("count of p :",count)
Output:
count of p : 2
Python List count() Method Example 2
If the passed value is not present in the list, the method returns 0. See the example below.
# Python list count() Method
# Creating a list
apple = ['a','p','p','l','e']
# Method calling
count = apple.count('b')
# Displaying result
print("count of b :",count)
Output:
count of b : 0
Python List extend() Method
Python extend() method extends the list by appending all the items from the iterable. Iterable can be a List, Tuple or a Set. Examples are described below.
Signature
extend(iterable)
Parameters
x: Iterable type parameter.
Return
It does not return any value rather modifies the list.
Let's see some examples of extend() method to understand it's functionality.
Python List extend() Method Example 1
It is a simple example to describe the use of extend method.
# Python list extend() Method
# Creating a list
list = ['1','2','3']
for l in list: # Iterating list
print(l)
list.extend('4')
print("After extending:")
for l in list: # Iterating list
print(l)
Output:
1
2
3
After extending:
1
2
3
4
Python List extend() Method Example 2
We can pass list as an element and the list will be extended. See the example below.
# Python list extend() Method
# Creating a list
list = ['1','2','3']
for l in list: # Iterating list
print(l)
list2 = ['4','5','6']
list.extend(list2)
print("After extending:")
for l in list: # Iterating list
print(l)
Output:
1
2
3
After extending:
1
2
3
4
5
6
Python List index() Method
Python index() method returns index of the passed element. This method takes an argument and returns index of it. If the element is not present, it raises a ValueError.
If list contains duplicate elements, it returns index of first occurred element.
This method takes two more optional parameters start and end which are used to search index within a limit.
Signature
index(x[, start[, end]])
Parameters
No parameter
Return
It returns None.
Let's see some examples of index() method to understand it's functionality.
Python List index() Method Example 1
Let's first see a simple example to get index of an element using index() method.
# Python list index() Method
# Creating a list
apple = ['a','p','p','l','e']
# Method calling
index = apple.index('p')
# Displaying result
print("Index of p :",index)
Output:
Index of p : 1
Python List index() Method Example 2
The method returns index of first element occurred, if the element is duplicate. See the following example.
# Python list index() Method
# Creating a list
apple = ['a','p','p','l','e']
# Method calling
index = apple.index('l') # 3
index2 = apple.index('p') # always 1
# Displaying result
print("Index of l :",index)
print("Index of p :",index2)
Output:
Index of l : 3
Index of p : 1
Python List pop() Method
Python pop() element removes an element present at specified index from the list. It returns the popped element. The syntax and signature is described below.
Signature
pop([i])
Parameters
x : Element to be popped. It is optional.
Return
It returns popped element
Python List pop() Method Example 1
Let's first see a simple example of popping element from the list. Element present at the index 2 is popped, see the example below.
# Python list pop() Method
# Creating a list
list = ['1','2','3']
for l in list: # Iterating list
print(l)
list.pop(2)
print("After poping:")
for l in list: # Iterating list
print(l)
Output:
1
2
3
After poping:
1
2
Python List pop() Method Example 2
The index is optional, if we don't specify the index, it pops element presents at the last index of the list. See the example below.
# Python list pop() Method
# Creating a list
list = ['1','2','3']
for l in list: # Iterating list
print(l)
list.pop()
print("After poping:")
for l in list: # Iterating list
print(l)
Output:
1
2
3
After poping:
1
2
Python List remove(x) Method
Python remove() method removes the first item from the list which is equal to the passed value. It throws an error if the item is not present in the list. Signature and examples are described below.
Signature
remove(x)
Parameters
x : Element to be deleted.
Return
It does not return any value rather modifies the list.
Let's see some examples of remove() method to understand it's functionality.
Python List remove() Method Example 1
A simple example in which we are removing an element from the list.
# Python list remove() Method
# Creating a list
list = ['1','2','3']
for l in list: # Iterating list
print(l)
list.remove('2')
print("After removing:")
for l in list: # Iterating list
print(l)
Output:
1
2
3
After removing:
1
3
Python List remove() Method Example 2
If list contains duplicate elements, the method will remove only first occurred element. See the example below.
# Python list remove() Method
# Creating a list
list = ['1','2','3','2']
for l in list: # Iterating list
print(l)
list.remove('2')
print("After removing:")
for l in list: # Iterating list
print(l)
Output:
1
2
3
2
After removing:
1
3
2
Python List reverse() Method
Python reverse() method reverses elements of the list. If the list is empty, it simply returns an empty list. After reversing the last index value of the list will be present at 0 index. The examples and method signature is given below.
Signature
reverse()
Parameters
No parameter
Return
It returns None.
Let's see some examples of reverse() method to understand it's functionality.
Python List reverse() Method Example 1
Let's first see a simple example to reverse the list. It prints all the elements in reverse order.
# Python list reverse() Method
# Creating a list
apple = ['a','p','p','l','e']
# Method calling
apple.reverse() # Reverse elements of the list
# Displaying result
print(apple)
Output:
['e', 'l', 'p', 'p', 'a']
Python List reverse() Method Example 2
It returns empty list if the list is the list is empty. See the example below.
# Python list reverse() Method
# Creating a list
apple = []
# Method calling
apple.reverse() # Reverse elements of the list
# Displaying result
print(apple)
Output:
[]
Python List sort() Method
Python sort() method sorts the list elements. It also sorts the items into descending and ascending order. It takes an optional parameter 'reverse' which sorts the list into descending order. By default, list sorts the elements into ascending order. The examples and signature are given below.
Signature
sort()
Parameters
No parameter
Return
It returns None.
Python List sort() Method Example 1
It is a simple example which sorts two lists in ascending order. See the example below.
# Python list sort() Method
# Creating a list
apple = ['a', 'p', 'p', 'l', 'e'] # Char list
even = [6,8,2,4] # int list
print(apple)
print(even)
# Calling Method
apple.sort()
even.sort()
# Displaying result
print("\nAfter Sorting:\n",apple)
print(even)
Output:
['a', 'p', 'p', 'l', 'e']
[6, 8, 2, 4]
After Sorting:
['a', 'e', 'l', 'p', 'p']
[2, 4, 6, 8]
Python List sort() Method Example 2
This example sorts the list into descending order.
# Python list sort() Method
# Creating a list
even = [6,8,2,4] # int list
# Calling Method
#apple.sort()
even.sort(reverse=True) # sort in reverse order
# Displaying result
print(even)
Output:
[8, 6, 4, 2]
Python append() method adds an item to the end of the list. It appends an element by modifying the list. The method does not return itself. The item can also be a list or dictionary which makes a nested list. Method is described below.
Signature
append(x)
Parameters
x: It can be a number, list, string, dictionary etc.
Return
It does not return any value rather modifies the list.
Python List append() Method Example 1
First, let's see a simple example which appends elements to the list.
# Python list append() Method
# Creating a list
list = ['1','2','3']
for l in list: # Iterating list
print(l)
# Appending element to the list
list.append(4)
print("List after appending element : ",list) # Displaying list
Output:
1
2
3
List after appending element : ['1', '2', '3', 4]
Python List append() Method Example 2
Appending a list to the list is also possible which will create a list inside the list. See the example below.
# Python list append() Method
# Creating a list
list = ['1','2','3']
for l in list: # Iterating list
print(l)
# Appending a list to the list
list2 = ['4','5','6','7']
list.append(list2)
print("List after appending element : ", list) # Displaying list
Output:
1
2
3
List after appending element : ['1', '2', '3', ['4', '5', '6', '7']]
Python List clear() Method
Python clear() method removes all the elements from the list. It clear the list completely and returns nothing. It does not require any parameter and returns no exception if the list is already empty. The syntax and examples are described below.
Signature
clear()
Parameters
No parameter
Return
It returns None.
Python List clear() Method Example 1
Let's see a simple example in which clear() method is used to clear a list. The clear() method clears all the elements from the list.
# Python list clear() Method
# Creating a list
list = ['1','2','3']
for l in list: # Iterating list
print(l)
list.clear()
print("After clearing:")
for l in list: # Iterating list
print(l)
Output:
1
2
3
After clearing:
Python List clear() Method Example 2
If the list is already empty, the method returns nothing. See the example below.
# Python list clear() Method
# Creating a list
list = []
for l in list: # Iterating list
print(l)
list.clear()
print("After clearing:")
for l in list: # Iterating list
print(l)
Output:
After clearing:
Python List copy() Method
Python copy() method copies the list and returns the copied list. It does not take any parameter and returns a list. The method signature and examples are given below.
Signature
copy()
Parameters
No parameter
Return
It returns a copy of the list.
Let's see some examples of copy() method to understand it's functionality.
Python List copy() Method Example 1
A Simple example which copy a list ot another and makes an new list. See the example below.
# Python list copy() Method
# Creating a list
evenlist = [6,8,2,4] # int list
copylist = []
# Calling Method
copylist = evenlist.copy()
# Displaying result
print("Original list:",evenlist)
print("Copy list:",copylist)
Output:
Original list: [6, 8, 2, 4]
Copy list: [6, 8, 2, 4]
Python List copy() Method Example 2
Here, we are using list slicing concept to create a copy of the list. See the example below.
# Python list copy() Method
# Creating a list
evenlist = [6,8,2,4] # int list
copylist = []
# Calling Method
copylist = evenlist[:] # Copy all the elements
# Displaying result
print("Original list:",evenlist)
print("Copy list:",copylist)
Output:
Original list: [6, 8, 2, 4]
Copy list: [6, 8, 2, 4]
next →← prev
Python List count() Method
Python count() method returns the number of times element appears in the list. If the element is not present in the list, it returns 0. The examples and syntax is described below.
Signature
count(x)
Parameters
x: element to be counted.
Return
It returns number of times x occurred in the list.
Let's see some examples of count() method to understand it's functionality.
Python List count() Method Example 1
It is a simple example to understand how a count method works.
# Python list count() Method
# Creating a list
apple = ['a','p','p','l','e']
# Method calling
count = apple.count('p')
# Displaying result
print("count of p :",count)
Output:
count of p : 2
Python List count() Method Example 2
If the passed value is not present in the list, the method returns 0. See the example below.
# Python list count() Method
# Creating a list
apple = ['a','p','p','l','e']
# Method calling
count = apple.count('b')
# Displaying result
print("count of b :",count)
Output:
count of b : 0
Python List extend() Method
Python extend() method extends the list by appending all the items from the iterable. Iterable can be a List, Tuple or a Set. Examples are described below.
Signature
extend(iterable)
Parameters
x: Iterable type parameter.
Return
It does not return any value rather modifies the list.
Let's see some examples of extend() method to understand it's functionality.
Python List extend() Method Example 1
It is a simple example to describe the use of extend method.
# Python list extend() Method
# Creating a list
list = ['1','2','3']
for l in list: # Iterating list
print(l)
list.extend('4')
print("After extending:")
for l in list: # Iterating list
print(l)
Output:
1
2
3
After extending:
1
2
3
4
Python List extend() Method Example 2
We can pass list as an element and the list will be extended. See the example below.
# Python list extend() Method
# Creating a list
list = ['1','2','3']
for l in list: # Iterating list
print(l)
list2 = ['4','5','6']
list.extend(list2)
print("After extending:")
for l in list: # Iterating list
print(l)
Output:
1
2
3
After extending:
1
2
3
4
5
6
Python List index() Method
Python index() method returns index of the passed element. This method takes an argument and returns index of it. If the element is not present, it raises a ValueError.
If list contains duplicate elements, it returns index of first occurred element.
This method takes two more optional parameters start and end which are used to search index within a limit.
Signature
index(x[, start[, end]])
Parameters
No parameter
Return
It returns None.
Let's see some examples of index() method to understand it's functionality.
Python List index() Method Example 1
Let's first see a simple example to get index of an element using index() method.
# Python list index() Method
# Creating a list
apple = ['a','p','p','l','e']
# Method calling
index = apple.index('p')
# Displaying result
print("Index of p :",index)
Output:
Index of p : 1
Python List index() Method Example 2
The method returns index of first element occurred, if the element is duplicate. See the following example.
# Python list index() Method
# Creating a list
apple = ['a','p','p','l','e']
# Method calling
index = apple.index('l') # 3
index2 = apple.index('p') # always 1
# Displaying result
print("Index of l :",index)
print("Index of p :",index2)
Output:
Index of l : 3
Index of p : 1
Python List pop() Method
Python pop() element removes an element present at specified index from the list. It returns the popped element. The syntax and signature is described below.
Signature
pop([i])
Parameters
x : Element to be popped. It is optional.
Return
It returns popped element
Python List pop() Method Example 1
Let's first see a simple example of popping element from the list. Element present at the index 2 is popped, see the example below.
# Python list pop() Method
# Creating a list
list = ['1','2','3']
for l in list: # Iterating list
print(l)
list.pop(2)
print("After poping:")
for l in list: # Iterating list
print(l)
Output:
1
2
3
After poping:
1
2
Python List pop() Method Example 2
The index is optional, if we don't specify the index, it pops element presents at the last index of the list. See the example below.
# Python list pop() Method
# Creating a list
list = ['1','2','3']
for l in list: # Iterating list
print(l)
list.pop()
print("After poping:")
for l in list: # Iterating list
print(l)
Output:
1
2
3
After poping:
1
2
Python List remove(x) Method
Python remove() method removes the first item from the list which is equal to the passed value. It throws an error if the item is not present in the list. Signature and examples are described below.
Signature
remove(x)
Parameters
x : Element to be deleted.
Return
It does not return any value rather modifies the list.
Let's see some examples of remove() method to understand it's functionality.
Python List remove() Method Example 1
A simple example in which we are removing an element from the list.
# Python list remove() Method
# Creating a list
list = ['1','2','3']
for l in list: # Iterating list
print(l)
list.remove('2')
print("After removing:")
for l in list: # Iterating list
print(l)
Output:
1
2
3
After removing:
1
3
Python List remove() Method Example 2
If list contains duplicate elements, the method will remove only first occurred element. See the example below.
# Python list remove() Method
# Creating a list
list = ['1','2','3','2']
for l in list: # Iterating list
print(l)
list.remove('2')
print("After removing:")
for l in list: # Iterating list
print(l)
Output:
1
2
3
2
After removing:
1
3
2
Python List reverse() Method
Python reverse() method reverses elements of the list. If the list is empty, it simply returns an empty list. After reversing the last index value of the list will be present at 0 index. The examples and method signature is given below.
Signature
reverse()
Parameters
No parameter
Return
It returns None.
Let's see some examples of reverse() method to understand it's functionality.
Python List reverse() Method Example 1
Let's first see a simple example to reverse the list. It prints all the elements in reverse order.
# Python list reverse() Method
# Creating a list
apple = ['a','p','p','l','e']
# Method calling
apple.reverse() # Reverse elements of the list
# Displaying result
print(apple)
Output:
['e', 'l', 'p', 'p', 'a']
Python List reverse() Method Example 2
It returns empty list if the list is the list is empty. See the example below.
# Python list reverse() Method
# Creating a list
apple = []
# Method calling
apple.reverse() # Reverse elements of the list
# Displaying result
print(apple)
Output:
[]
Python List sort() Method
Python sort() method sorts the list elements. It also sorts the items into descending and ascending order. It takes an optional parameter 'reverse' which sorts the list into descending order. By default, list sorts the elements into ascending order. The examples and signature are given below.
Signature
sort()
Parameters
No parameter
Return
It returns None.
Python List sort() Method Example 1
It is a simple example which sorts two lists in ascending order. See the example below.
# Python list sort() Method
# Creating a list
apple = ['a', 'p', 'p', 'l', 'e'] # Char list
even = [6,8,2,4] # int list
print(apple)
print(even)
# Calling Method
apple.sort()
even.sort()
# Displaying result
print("\nAfter Sorting:\n",apple)
print(even)
Output:
['a', 'p', 'p', 'l', 'e']
[6, 8, 2, 4]
After Sorting:
['a', 'e', 'l', 'p', 'p']
[2, 4, 6, 8]
Python List sort() Method Example 2
This example sorts the list into descending order.
# Python list sort() Method
# Creating a list
even = [6,8,2,4] # int list
# Calling Method
#apple.sort()
even.sort(reverse=True) # sort in reverse order
# Displaying result
print(even)
Output:
[8, 6, 4, 2]
Comments
Post a Comment