List Methods in Python | Set 1 (in, not in, len(), min(), max()…)
List basics have been covered in python in the set below
Data Types-List, Tuples and Iterations
List methods are discussed in this articles.
1. “in” operator :- This operator is used to check if an element is present in the list or not. Returns true if element is present in list else returns false.
2. “not in” operator :- This operator is used to check if an element is not present in the list or not. Returns true if element is not present in list else returns false.
Python3
# Python code to demonstrate the working of # "in" and "not in" # initializing list lis = [ 1 , 4 , 3 , 2 , 5 ] # checking if 4 is in list using "in" if 4 in lis: print ( "List is having element with value 4" ) else : print ( "List is not having element with value 4" ) # checking if 4 is not list using "not in" if 4 not in lis: print ( "List is not having element with value 4" ) else : print ( "List is having element with value 4" ) |
Output:
List is having element with value 4 List is having element with value 4
3. len() :- This function returns the length of list.
4. min() :- This function returns the minimum element of list.
5. max() :- This function returns the maximum element of list.
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)) |
Output: