Method | Description |
---|---|
append() | To add a new item to the end of the list. |
clear() | To remove all the elements from the list. |
copy() | Returns a new copy of the list. |
count() | Count the number of times an item is repeated in the list. |
extends() | To merge one list to another. |
index() | Index position of the first appearance of the item. |
insert() | To insert an element at any particular index position in the list. |
len() | To find the length of the list. |
pop() | Remove an item from the list. |
remove() | Removes the specific item from the list using the value of the item. |
reverse() | To reverse the elements of the list. |
sort() | To sort the items inside the list. |
Python List append()
Method
Python List append()
can be used to add a new item to the end of the list.
emp_details = ['Brad','Lee','Sales',380,250000,True]
emp_details.append('USA')
emp_details
Python List clear()
Method
Python List clear()
will remove all the elements from the list.
emp_details = ['Brad','Lee','Sales',380,250000,True]
emp_details.clear()
emp_details
Python List copy()
Method
Python List copy()
returns a new copy of the list. Though it is not mandatory to use the copy function, we can directly assign the list to another variable and it will create a new list.
With copy() funciton.
emp_details = ['Brad','Lee','Sales',380,250000,True]
emp_copy = emp_details.copy()
emp_copy
Without copy() function.
emp_without_copy = emp_details
emp_without_copy
Python List count()
Method
Count the number of times an item is repeated in the list.
emp_details = ['Brad','Lee','USA','Sales',380,250000,True,'USA']
emp_details.count('USA')
Python List extends()
Method
Extends is used when we have two lists and we want each element of the second list to be merged in the first one. Note: Items we want to merge can be any iterable, not mandatorily list.
emp_details = ['Brad','Lee','USA','Sales',380,250000,True]
emp_address = ['New York','USA']
emp_details.extend(emp_address)
emp_details
Python List index()
Method
Returns the index position of the first appearance of the item from the list. If the item is not found, it returns as an error. It has an optional start and end index to search within that specified range. The default value of the start index is 0 and the end index is the end of the string.
Note: Here end index is up to the index position. Item at end index position is not included.
The first index position is returned when the item is found in the list.
emp_details = ['Brad','Lee','USA','Sales',380,250000,True,'Sales']
emp_details.index('Sales')
Returns error when the item is not found in the list.
emp_details = ['Brad','Lee','USA','Sales',380,250000,True]
emp_details.index('Peter')
Using the start and end index positions.
emp_details = ['Brad','Lee','USA','Sales',380,250000,True]
emp_details.index('Sales',2,4)
When item not found within index range.
emp_details = ['Brad','Lee','USA','Sales',380,250000,True]
emp_details.index('Sales',0,2)
Python List insert()
Method
To insert an element at any particular index position in the list.
emp_details = ['Brad','Lee','Sales',380,250000,True]
emp_details.insert(2,'USA')
emp_details
Python List len()
Method
To find the length of the list we use len()
function.
emp_details = ['Brad','Lee','Sales',380,250000,True,'USA']
len(emp_details)
Python List pop()
Method
It will remove an item from the list. By default it will remove the last item from the list but we do have an option to specify the index. we can save the pop item in the variable.
In this example output is ‘USA’, the item it poped.
emp_details = ['Brad','Lee','Sales',380,250000,True,'USA']
emp_details.pop()
List after pop.
emp_details
If we want to pop ‘Sales’ from emp_details list, we can specify the index location
emp_details = ['Brad','Lee','Sales',380,250000,True,'USA']
item_poped = emp_details.pop(2)
item_poped
List after pop.
emp_details
Python List remove()
Method
Removes the specific item from the list using the value of the item. If the item is repeated several times then only the first occurrence is removed.
Note: Only the first occurrence of ‘Sales’ is removed.
emp_details = ['Brad','Lee','Sales',380,250000,True,'Sales']
emp_details.remove('Sales')
emp_details
Python List reverse()
Method
To reverse the elements of the list.
emp_details = ['Brad','Lee','Sales',380,250000,True,'USA']
emp_details.reverse()
emp_details
Python List sort()
Method
To sort the items inside the list.
my_list = ['X','c','C','c','r','a','d','b']
my_list.sort()
my_list