List of Methods in Python | Set 1 (in, not in, len (), min (), max () …)

Python Methods and Functions

List, Tuple and Iteration Data Types

The list of methods is discussed in this article.

1. The "in" operator : — This operator is used to check for an element in the list. Returns true if the item is in the list, otherwise returns false.

2. Not in operator : — This operator is used to check if an item is missing from the list or not. Returns true if the element is not in the list, otherwise returns false.

# Python code to demonstrate how it works
# & quot; in & quot; and & quot; not in & quot;
# initializing list

lis = [ 1 , 4 , 3 , 2 , 5 ]

 
# check if 4 is in the 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 " )

  
# check if 4 is not in the list using & quot; not in & quot;

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 ha ving element with value 4 List is having element with value 4 

3. len () : — This function returns the length of the list.

4. min () : — This function returns the minimum list item.

5. max () : — This function returns the maximum list item.

# Python code to demonstrate how it works
# len (), min () and max ()
# initialize list 1

lis = [ 2 , 1 , 3 , 5 , 4 ]

 
# using len () to print the length of the list

print ( " The length of list is: " , end = "")

print ( len (lis))

 
# using min () to display the minimum list item

print ( "The minimum element of list is:" , end = "")

print ( min ( lis))

 
# using max () to print the maximum list item

print ( "The maximum element of lis t is: " , end = " ")

print ( max (lis))

Output:

 The length of list is: 5 The minimum element of list is: 1 The maximum element of list is: 5 

6. Operator "+" : — This operator is used to combine two lists into one list.

7. Operator "*" : — This operator is used to multiply a list n times and return one list.

# Python code to demonstrate how it works
# & quot; + & quot; and & quot; * & quot;
# initialize list 1

lis = [ 1 , 2 , 3 ]

 
# initializing list 2

lis1 = [ 4 , 5 , 6 ]

 
# using & quot; + & quot; to combine lists

lis2 = lis + lis1

 
# printing multiple lists

print ( "list after concatenation is:" , end = "")

for i in range ( 0 , len (lis2)):

print (lis2 [i], end = "" )

 

print ( "" )

 
# using & # 39; * & # 39; to combine lists

lis3 = lis * 3

 
# printing combo boxes

print ( "list after combining is:" , end = "")

for i in range ( 0 , len (lis3)):

print (lis3 [i], end = ""  )

Output:

 list after concatenation is: 1 2 3 4 5 6 list after combining is: 1 2 3 1 2 3 1 2 3 

8. index (ele, beg, end) : — This function returns the index of the first occurrence of an element from the beginning to the end.

9. count () : — this function counts the number of occurrences of elements in the list.

# Python code to demonstrate how it works
# index () and count ()
# list 1 initialization

lis = [ 2 , 1 , 3 , 5 , 4 , 3 ]

 
# using index () to print the first occurrence of 3
# prints 5 < / p>

print ( "The first occurrence of 3 after 3rd position is : " , end = " ")

print (lis.index ( 3 , 3 , 6 ))

 
# using count ( ) to count the number of occurrences 3

print ( "The number of occurrences of 3 is:" , end = "")

print (lis.count ( 3 ))

Output:

 The first occurrence of 3 after 3rd position is: 5 The number of occurrences of 3 is: 2 

Related Articles:
List of methods in Python
List of Methods in Python | Set 2 (del, remove (), sort (), insert (), pop (), extend () ...)

This article is contributing to Manjeet Singh If you love Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or post the article [email protected] See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.





Tutorials