Other programming languages use a concept of array.Concept of List in Python work as a data structure,which is used to store various data.
Python list enclosed between square brackets.
list_name=[first,second,third,....n];
list1 = ["Ram","Rahul","Mohit"]; list2 = ['A','B','C','D']; list3 = [1,2,3,4]; list4 = [1234,"Name",'a',2.45];
List example
list1 = ["Rahul","Mohit","Romit"]; print list[2]
output: Romit
Adding or joining two list elements.
list1 = [1,2,3]; list2 = [4,5,6]; list3 = list1+list2 print (list3)
output: [1,2,3,4,5,6]
Repeating list element again again using '*'.
list=[1,1] print list*2
output: [1,1,1,1]
list =[1,2,3,4,5,7] print list[0:2]
output: [1,4]
To update or change the value of list.
item=[2,4,8,10] print "list are: " print item item2[2]="New Element" print "Values of list are: " print item2
output:
list are:[2,4,8,10]
Values of list are: [2,4,"New Element",10]
del statement can be used to delete an element from the list.
list=[1,"rahul",2,'a',3] print list del list[1] print list
output:
[1,2,'a',3]
append() method is used to add an element at the end of the existing elements.
list=[1,2,"Rohit"] list.append(3) print "List after appending: " print list
output: List after appending:
[1,2,'a',3]