List Operations using python

List Operations

Code:

 #list example

list1=["a","b","c","d","e"]
print (list1)
print("\n data type for list1:\t",type(list1))

#finding index number

x=list1.index("c")
print("\nindex of x: ",x)

#append element
list1.append(1)
print("\nlist1=",list1)

#index numbers
print("\nlist1[0]=",list1[0],"\nlist1[2]=",list1[2])

#insert new element in list
list1.insert(10,"zggggg")
print("\nupdated list: ",list1)

#update list element
list1.insert(2,"xyz")
print("Updated list=",list1)
Output:
['a', 'b', 'c', 'd', 'e']

 data type for list1:	 <class 'list'>

index of x:  2

list1= ['a', 'b', 'c', 'd', 'e', 1]

list1[0]= a 
list1[2]= c

updated list:  ['a', 'b', 'c', 'd', 'e', 1, 'zggggg']
Updated list= ['a', 'b', 'xyz', 'c', 'd', 'e', 1, 'zggggg']


List operations:


#defining list
list1=[1,2,3,4,5]
list2=[11,22,33,44,55]
print("list1=",list1,"\nlist2=",list2)
#popping elements
list1.pop()
list2.pop()
print("list1=",list1,"\nList2=",list2)

#deletion
del list1[1]
print("list1 after del list1[1]",list1)
#remove
list1.remove(1)
print("After list1.remove(1)=",list1)
#copy
list_rev=list2.copy()
print("copied list=",list_rev)

#using list constructor
list3=list(list2)
print("using list constructor list3=list(list2)=",list3)

output:
list1= [1, 2, 3, 4, 5] 
list2= [11, 22, 33, 44, 55]
list1= [1, 2, 3, 4] 
List2= [11, 22, 33, 44]
list1 after del list1[1] [1, 3, 4]
After list1.remove(1)= [3, 4]
copied list= [11, 22, 33, 44]
using list constructor list3=list(list2)= [11, 22, 33, 44]


Comments

Popular posts from this blog

Tuple using Python: