List Datatype in Python

List is a mutable datatype in python.It is written in square bracket [ ].

Example
 li=["Vikas" , "Small Code" , 12]

Indexing of List

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".

Example
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
 

Accessing the element of List

If we want to access the element of list use the index value.like

Example
li=["Vikas" , "Small Code" , 12]
print(li[1])
print(li[-3])
Output
Small Code
Vikas

Changing the value of element in list

If we want to change the value of elements in list we just assign the value at a particular index.

Example
li=["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

Existing of element in list

If we want to check the element is belong to the list or not use in function.

Example
 li=["Vikas" , "Small Code" , 12]
 if "Vikas" in li:
    print("Element is Present")
 else:
    print("Element is not present")
Output
Element is Present

Print the list in output screen

If we want to print the list we just print the name of list.

Example
li=["Vikas" , "Small Code" , 12]
print(li)
Output
["Vikas" , "Small Code" , 12]

Print the length of list

If we want to find how many element in list use len() function.

Example
li=["Vikas" , "Small Code" , 12]
print(len(li))
Output
3

Adding element in List

"append()" function are used to add new element at last in list.

Example
li=["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 element of List

"remove()" function are used to remove the element of list.

Example
li=["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.

Example
li=["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.

Example
li=["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 the list

"copy()" function are used to copy the list.

Example
li1=["Vikas" , "Small Code" , 12]
li2=li1.copy()
print(li2)
Output
["Vikas" , "Small Code" , 12]

Other function and use

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.