List Methods in Python | Set 1 (in, not in, len(), min(), max()...)
Last Updated :
03 Aug, 2022
List methods are discussed in this article.
1. len() :- This function returns the length of list.
List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1] print(len(List)) Output: 10
2. min() :- This function returns the minimum element of list.
List = [2.3, 4.445, 3, 5.33, 1.054, 2.5] print(min(List)) Output: 1.054
3. max() :- This function returns the maximum element of list.
List = [2.3, 4.445, 3, 5.33, 1.054, 2.5] print(max(List)) Output: 5.33
Python3
# Python code to demonstrate the working of
# len(), min() and max()
# initializing list 1
lis = [2, 1, 3, 5, 4]
# using len() to print length of list
print ("The length of list is : ", end="")
print (len(lis))
# using min() to print minimum element of list
print ("The minimum element of list is : ", end="")
print (min(lis))
# using max() to print maximum element of list
print ("The maximum element of list is : ", end="")
print (max(lis))
OutputThe length of list is : 5
The minimum element of list is : 1
The maximum element of list is : 5
Output:
The length of list is : 5
The minimum element of list is : 1
The maximum element of list is : 5
4. index(ele, beg, end) :- This function returns the index of first occurrence of element after beg and before end.
List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1] print(List.index(2)) Output: 1
5. count() :- This function counts the number of occurrences of elements in list.
List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1] print(List.count(1)) Output: 4
Python3
# Python code to demonstrate the working of
# index() and count()
# initializing list 1
lis = [2, 1, 3, 5, 4, 3]
# using index() to print first occurrence of 3
# prints 5
print ("The first occurrence of 3 after 3rd position is : ", end="")
print (lis.index(3, 3, 6))
# using count() to count number of occurrence of 3
print ("The number of occurrences of 3 is : ", end="")
print (lis.count(3))
OutputThe first occurrence of 3 after 3rd position is : 5
The number of occurrences of 3 is : 2
Similar Reads
Use of min() and max() in Python Prerequisite: min() max() in Python Let's see some interesting facts about min() and max() function. These functions are used to compute the maximum and minimum of the values as passed in its argument. or it gives the lexicographically largest value and lexicographically smallest value respectively,
2 min read
List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()...) Some of the list methods are mentioned in set 1 below More methods are discussed in this article. 1. del[a : b] :- This method deletes all the elements in range starting from index 'a' till 'b' mentioned in arguments. 2. pop() :- This method deletes the element at the position mentioned in its argum
4 min read
Maximum and Minimum element in a Set in Python We are given a set and our task is to find the maximum and minimum elements from the given set. For example, if we have a = {3, 1, 7, 5, 9}, the maximum element should be 9, and the minimum element should be 1.Letâs explore different methods to do it:1. Using min() & max() functionThe built-in m
3 min read
Index of Non-Zero Elements in Python list We are given a list we need to find all indexes of Non-Zero elements. For example, a = [0, 3, 0, 5, 8, 0, 2] we need to return all indexes of non-zero elements so that output should be [1, 3, 4, 6].Using List ComprehensionList comprehension can be used to find the indices of non-zero elements by ite
2 min read
Python | Set 4 (Dictionary, Keywords in Python) In the previous two articles (Set 2 and Set 3), we discussed the basics of python. In this article, we will learn more about python and feel the power of python. Dictionary in Python In python, the dictionary is similar to hash or maps in other languages. It consists of key-value pairs. The value c
5 min read