Python List




1.       Write a program to create a list using range functions and perform append, update and delete elements operations in it

lst=list(range(1,6))
print("Original list is ", lst)
lst.append(8)
print(lst)
del lst[0]
print(lst)
lst.remove(8)
print(lst)
lst.insert(0,99)
print("after insert method", lst)
#printing list using for loop
print(" Print List with loop")
for n in lst:
    print(n)

2.Write a program to combine two List, perform repetition of lists and create cloning of lists.

#concatenatoion of two list
l1=[1,2,3]
l2=['a','b']
l3=l1+l2
print(l3)
#Repetition of list
l4=l3*2
print(l4)
#alias and cloning the list
print("Alias")
l1=[1,2,3,4,5]
l2=l1
print("l1 is", l1)
print("l2 is", l2)
l2[0]=20
print("l1 is", l1)
print("l2 is", l2)
print("Cloning")
l1=[1,2,3,4,5]
l2=l1[:]
print("l1 is", l1)
print("l2 is", l2)
l2[0]=20
print("l1 is", l1)
print("l2 is", l2)

3.Create a sample list of 7 elements and implement the List methods mentioned: append, insert, copy, extend, count, remove, pop, sort, reverse and clear.

l1=[1,2,3,4,5,6,7]
print(l1);
print("Sum method", sum(l1))
print("length method", len(l1))
print("Index of 5 in list",l1.index(5))
l2=[6,7]
l1.append(l2)
print("append method", l1)
l1.append(8)
print("append method", l1)
l1.insert(0,99)
print("after insert method", l1)


l3=l1.copy()
print("copy method", l3)

l4=[10,20]
l4.extend(l1)
print("extend method", l4)

print("count method 1 in list1", l1.count(1))

l4.remove([6,7])
print("remove method", l4)

l5=[1,2,3,4,5,6,7]
print("pop method", l5.pop())

l5.reverse()
print("reverse method", l5)

l5.sort()
print("sort method")
print(l5)

l5.clear()
print("Clear method")
print(l5)

4.Write a program to create nested list and display its elements.

l1=[1,2,3]
l2=[4,5,6,l1]
print("list 1",l1)
print("list 2",l2)

print("Complete list")
for i in l2:
    print(i)

print("Elements from nested list")   
for i in l2[3]:
    print(i)