List in Python programming | Hands on coding

What is list?
  • It is a collection of data of different data type.
  •  It is used to store list of values.
  •  A list is created by putting list of comma-separated values between square brackets.

str_list=["Apple","Orange","Mango"]
int_list=[15,25,36,84,59]
float_list=[2.3,5.6,1.4,9.6]
mixed_list=["Easy",205,25.3]
  • Value of list can be accessed using index number.

fruit_list=["Apple","Orange","Mango"]
print("I like ",fruit_list[0])
print("I like ",fruit_list[2])
"""
***Output***
I like  Apple
I like  Mango
"""

int_list=[5,10,15,20,25,30,35,40,45,50]
#Print will start at index 1 and end at index 4.
print(int_list[1:4])
"""
***Output***
[10, 15, 20]
"""
  • Negative indexes start from the end of the list.
  • Negative index always starts with -1.

fruit_list=["Apple","Orange","Mango"]
print(fruit_list[-3])#Apple
print(fruit_list[-2])#Orange
print(fruit_list[-1])#Mango
"""
***Output***
Apple
Orange
Mango
"""


fruit_list=["Apple","Orange","Mango"]
for name in fruit_list:
    print("I like ",name)
"""
***Output***
I like  Apple
I like  Orange
I like  Mango
"""


fruit_list=["Apple","Orange","Mango"]
print("Before Updation")
print(fruit_list)
#this line will replace Orange with Banana
fruit_list[1]="Banana"
print("After Updation")
print(fruit_list)
"""
***Output***
Before Updation
['Apple', 'Orange', 'Mango']
After Updation
['Apple', 'Banana', 'Mango']
"""
  • len() function is used to get length of list.

fruit_list=["Apple","Orange","Mango"]
print("Length of list is ",len(fruit_list))
"""
***Output***
Length of list is  3
"""
  • append() function is used to add new items into list.

fruit_list=["Apple","Orange","Mango"]
print("Before insertion")
print(fruit_list)
#this line will add  Banana at the end of list
fruit_list.append("Banana")
print("After insertion")
print(fruit_list)
"""
***Output***
Before insertion
['Apple', 'Orange', 'Mango']
After insertion
['Apple', 'Orange', 'Mango', 'Banana']
"""
  • insert() function is used to add new items into list at particular index.

fruit_list=["Apple","Orange","Mango"]
print("Before insertion")
print(fruit_list)
#this line will add  Banana at index 1
fruit_list.insert(1,"Banana")
print("After insertion")
print(fruit_list)
"""
***Output***
Before insertion
['Apple', 'Orange', 'Mango']
After insertion
['Apple', 'Banana', 'Orange', 'Mango']
"""
  • remove() function  used to remove item from list.

fruit_list=["Apple","Orange","Mango"]
print("Before deletion")
print(fruit_list)
#this line will delete  Orange from list
fruit_list.remove("Orange")
print("After deletion")
print(fruit_list)
"""
***Output***
Before deletion
['Apple', 'Orange', 'Mango']
After deletion
['Apple', 'Mango']
"""
  • pop() function is used to delete or remove item from list using index.

fruit_list=["Apple","Orange","Mango"]
print("Before deletion")
print(fruit_list)
#this line will delete  Orange from list
fruit_list.pop(1)
print("After deletion")
print(fruit_list)
"""
***Output***
Before deletion
['Apple', 'Orange', 'Mango']
After deletion
['Apple', 'Mango']
"""


fruit_list=["Apple","Orange","Mango"]
print("Before deletion")
print(fruit_list)
#this line will delete  Mango from list
fruit_list.pop()
print("After deletion")
print(fruit_list)
"""
***Output***
Before deletion
['Apple', 'Orange', 'Mango']
After deletion
['Apple', 'Orange']
"""


fruit_list=["Apple","Orange","Mango"]
print("Before deletion")
print(fruit_list)
#this line will delete  Orange from list
del fruit_list[1]
print("After deletion")
print(fruit_list)
"""
***Output***
Before deletion
['Apple', 'Orange', 'Mango']
After deletion
['Apple', 'Mango']
"""


fruit_list=["Apple","Orange","Mango"]
print("List Items")
print(fruit_list)
#this line will delete  all the items of the list
del fruit_list
print("Deleted successfully")
"""
***Output***
List Items
['Apple', 'Orange', 'Mango']
Deleted successfully
"""
  • clear() function is used to clear or empty the list.

fruit_list=["Apple","Orange","Mango"]
print("Before clear")
print(fruit_list)
#this line will empty the list
fruit_list.clear()
print("After clear")
print(fruit_list)
"""
***Output***
Before clear
['Apple', 'Orange', 'Mango']
After clear
[]
"""
  • copy() function is used to copy one list into another.


list1=["Apple","Orange","Mango"]
print("list1 items")
print(list1)
#this line will copy list1 items into list2
list2=list1.copy()
print("list2 items")
print(list2)
"""
***Output***
list1 items
['Apple', 'Orange', 'Mango']
list2 items
['Apple', 'Orange', 'Mango']
"""
  • We can join two list using plus(+) operator.

list1=["Apple","Orange","Mango"]
list2=["Cherry","Grapes","Melon"]
#this line will join list1 and list2
list3=list1+list2
print("list3 items")
print(list3)
"""
***Output***
list3 items
['Apple', 'Orange', 'Mango', 'Cherry', 'Grapes', 'Melon']
"""
  • extend() function is also used to join two list.

list1=["Apple","Orange","Mango"]
list2=["Cherry","Grapes","Melon"]
#this line will join list1 and list2
list1.extend(list2)
print("list1 items")
print(list1)
"""
***Output***
list1 items
['Apple', 'Orange', 'Mango', 'Cherry', 'Grapes', 'Melon']
"""
  • append() function is also used to join two list.

list1=["Apple","Orange","Mango"]
list2=["Cherry","Grapes","Melon"]

for name in list2:
  list1.append(name)
print("list1 items")
print(list1)
"""
***Output***
list1 items
['Apple', 'Orange', 'Mango', 'Cherry', 'Grapes', 'Melon']
"""