Monday, 30 December 2019

lists methods



pop():
It is a predefined function, which is used to delete the values from the list.  It returns the deleted value.
Use index number to delete particular element.
If you are not using the index value, last element is deleted.
#pop()
a=[2,3,6,20] #list is created with elements
print(a)
c=a.pop()#it deletes the last element, and returns
print(c)
"""
output:
[2,3,6,20]
20
"""
Removing a particular item from the nested list : using remove()
a=[[10,20,30],[40,50,60]]
print(a)
print(a[1][0])#40
#a[1].remove(50) #[[10,20,30],[40,50,60]]
print("after removing")
print(a)


"""
output:
[[10, 20, 30], [40, 50, 60]]
"""
Removing a particular element from the nested list using del operator
a=[[10,20,30],[40,50,60]]
print(a)
print(a[1][0])#40
del a[1][2]#[[10, 20, 30], [40, 50]]
print("after removing")
print(a)

"""
output:
[[10, 20, 30], [40, 50]]
"""

#find the smallest and largest value in the list
a=[10,20,78,90,4,30] #list is created
print(a)
print(min(a))# 4
print(max(a))#90

index():
it is a predefined function, it used to find the index value of the particular element.
# search the particular element in the list and find the element
a=[10,20,78,90,4,30] #list is created
print(a)
print(a.index(90))

Comparing the two lists:
a,b=[10,20,30,40],[10,20,30,40] #two lists are created
#a=[10,20,30,40]
#b=[10,20,30,40]
print(id(a))
print(id(b))
print(a==b) #comparing the two lists
"""
output:
58443336
58443016
True
"""

Example2:
a,b=[10,20,30,40],[30,20,10,40] #two lists are created
#a=[10,20,30,40]
#b=[10,20,30,40]
print(id(a))
print(id(b))
print(a==b) #comparing the two lists
"""
output:
55952848
55952528
False
"""

By using the comparator operator, it compared the two content of the objects with ordered list

Comparing the two lists with the help of sort() function:
a,b=[10,20,30,40],[30,20,10,40] #two lists are created
#a=[10,20,30,40]
#b=[10,20,30,40]
print(id(a))
print(id(b))
print(a.sort()==b.sort())
"""
output:
49923496
49923176
True
"""

#write a program no of times 'n' appears in the list
a=["rama","sitha","ravi","venu","ravi"]
print(a.count("ravi"))

"""
2
"""


#write a program, sort the values in reverse and non-reverse values
a=[10,30,60,80,5,20]
print("Before sorting")
print(a)
#a.reverse()
#print(a)
print("after sorting, where reverse=True")
a.sort(key=None,reverse=True)
print("after sorting, where reverse=False")
a.sort(key=None,reverse=False)
print(a)
"""
output:
Before sorting
[10, 30, 60, 80, 5, 20]
after sorting, where reverse=True
[80, 60, 30, 20, 10, 5]
"""
#by using the negative index nos we can reverse the list:
#write a program, sort the values in reverse and non-reverse values
a=[10,30,60,80,5,20]
print("Before sorting")
print(a)
print("after performing reverse order")
print(a[::-1])

"""

Before sorting
[10, 30, 60, 80, 5, 20]
after performing
[20, 5, 80, 60, 30, 10]
"""
Related video: https://youtu.be/J56YFtcSykQ

 Next topic: Tuple

No comments:

Post a Comment

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(...