# Define a list my_list = [1, 2, 3, 4, 5] # Get the number of elements in the list num_elements = len(my_list) # Print the number of elements print(num_elements)
5
To get the number of elements in a list in Python, you can use the len() function. This function returns the number of elements in the list.
You can also use the count() method of the list to get the number of occurrences of a specific element in the list. This method returns the number of times the specified element appears in the list. Here is an example:# Define a list my_list = [1, 2, 3, 4, 5, 2, 3, 4, 5] # Get the number of occurrences of the number 2 in the list num_occurrences = my_list.count(2) # Print the number of occurrences print(num_occurrences)This will print 2 in the console, which is the number of times the number 2 appears in the my_list list.
Learn Flask development and learn to build cool apps with our premium Python course on Udemy.