More method in list
Making a copy of a list
a=[1,2,3,4,5]
b=a
print(a)
print(b)
Output:
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
Function copy() :
This function helps us to copy a list to another list
a=[1,2,3,4,5]
b=a.copy()
print(a)
print(b)
Output:
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
Function len()
This function helps us to find total elements in the list.
a=[1,2,3,4,5]
print(a)
print(len(a))
Output:
[1, 2, 3, 4, 5]
5
n=[‘a’,’b’,’c’]
print(n)
print(“total elements = “,len(n))
Output:
[‘a’, ‘b’, ‘c’]
total elements = 3
Function index():
This function returns the index of the first matched item/element from the list.
syntax:
list.index(item)
n=[1,2,3,4,5,6,7]
print(n)
print(n.index(5))
Output:
[1, 2, 3, 4, 5, 6, 7]
4
n=[1,2,3,4,5,6,7]
print(n)
print(n.index(2))
Output:
[1, 2, 3, 4, 5, 6, 7]
1
Function sorted():
This function takes the name of a list as an argument and returns a new sorted list with sorted elements.
syntax:
list1=sorted(list2)
a=[2,6,5,1,4]
print(a)
b=sorted(a)
print(b)
Output:
[2, 6, 5, 1, 4]
[1, 2, 4, 5, 6]
function sort():
This function helps us to sort the elements of a list.
syntax:
list.sort()
a=[2,6,5,1,4]
print(a)
a.sort()
print(a)
Output:
[2, 6, 5, 1, 4]
[1, 2, 4, 5, 6]
Function max()
This function helps us to find the largest element from the list elements.
syntax:
max(list)
a=[2,6,5,1,4,45,65,3,23]
print(a)
print(max(a))
Output:
[2, 6, 5, 1, 4, 45, 65, 3, 23]
65
Function min()
This function helps us to find the lowest element from the list elements.
syntax:
min(list)
a=[2,6,5,1,4,45,65,3,23]
print(a)
print(min(a))
Output:
[2, 6, 5, 1, 4, 45, 65, 3, 23]
1
Function sum()
This function helps us to find the sum of all element in the list elements.
syntax:
sum(list)
a=[2,6,5,1,4,45,65,3,23]
print(a)
print(sum(a))
Output:
[2, 6, 5, 1, 4, 45, 65, 3, 23]
154