List is a mutable datatype in python.It is written in square bracket [ ].
Exampleli=["Vikas" , "Small Code" , 12]
Indexing of list in both direction left to right and left.
Left to right have positive indexing and right to left is negative indexing.
Positive indexing start from "0" and negative start from "-1".
li=[5 , 10 , 15, 20, 25, 30, 35, 40, 45] Positive indexing 0 1 2 3 4 5 6 7 8 Negative indexing 9 -8 -7 -6 -5 -4 -3 -2 -1
If we want to access the element of list use the index value.like
Exampleli=["Vikas" , "Small Code" , 12] print(li[1]) print(li[-3])Output
Small Code Vikas
If we want to change the value of elements in list we just assign the value at a particular index.
Exampleli=["Vikas" , "Small Code" , 12] print("Value Before change") print(li[0]) li[0]="Vicky" print("Value After change") print(li[0])Output
Value Before change Vikas Value After change Vicky
If we want to check the element is belong to the list or not use in function.
Exampleli=["Vikas" , "Small Code" , 12] if "Vikas" in li: print("Element is Present") else: print("Element is not present")Output
Element is Present
If we want to print the list we just print the name of list.
Exampleli=["Vikas" , "Small Code" , 12] print(li)Output
["Vikas" , "Small Code" , 12]
If we want to find how many element in list use len() function.
Exampleli=["Vikas" , "Small Code" , 12] print(len(li))Output
3
"append()" function are used to add new element at last in list.
Exampleli=["Vikas" , "Small Code" , 12] print("Element before added") print(li) li.append("Sangam") print("Element after added") print(li)Output
Element before added ["Vikas" , "Small Code" , 12] Element after added ["Vikas" , "Small Code" ,12 ,"Sangam"]
"remove()" function are used to remove the element of list.
Exampleli=["Vikas" , "Small Code" , 12] print("Element before remove.") print(li) li.remove(12) print("Element after remove.") print(li)Output
Element before remove. ["Vikas" , "Small Code" , 12] Element after remove. ["Vikas" , "Small Code"]
"pop()" function are used to remove the element at last in list.
Exampleli=["Vikas" , "Small Code" , 12] print("Element before pop.") print(li) li.pop() print("Element after pop.") print(li)Output
Element before pop. ["Vikas" , "Small Code" , 12] Element after pop. ["Vikas" , "Small Code"]
"del()" function are used to remove the element at the perticular index of list.
Exampleli=["Vikas" , "Small Code" , 12] print("Element before delete.") print(li) li.del(2) print("Element after delete.") print(li)Output
Element before delete. ["Vikas" , "Small Code" , 12] Element after delete. ["Vikas" , "Small Code"]
"copy()" function are used to copy the list.
Exampleli1=["Vikas" , "Small Code" , 12] li2=li1.copy() print(li2)Output
["Vikas" , "Small Code" , 12]
function | Discription |
---|---|
count( ) | To count the number of element in list. |
clear( ) | Remove all the element of list. |
extend( ) | add the new list in the current list. |
insert( ) | Adding element at spacific position. |
reverse( ) | Use to reverse the list. |
sort( ) | use to short the list. |
'+' Operator | Join the two list. |