How to get number of elements in a list – Python ?
This tutorial guides you on how to get number of elements in a list using Python programming. Also you will learn how to get unique number of elements and test whether the given list is empty or not in a Pythonic way.
Get number of elements in a list – Python
Below example shows how to use len(seq) function to get the number of elements in a list. This Python built-in function returns the length (the number of elements) of an object. Note, the argument can be any sequence like string, bytes, tuple, list etc.,
For example,
my_first_list = [10, 20, 30, 40, 50, 11, 22, 33, 44, 55, 66, 77] my_second_list = []
length
len(my_first_list) 12 len(my_second_list) 0
Using for loop
The following example shows another way to get number of elements in a list using a for loop.
my_first_list = [10, 20, 30, 40, 50, 11, 22, 33, 44, 55, 66, 77] def get_number_of_elements(list): count = 0 for element in list: count += 1 return count print("Number of elements in the list: ", get_number_of_elements(my_first_list))
Output
Number of elements in the list: 12
In the above example, we first initialize the count (number of elements) to ‘0‘. And inside for loop the count will be incremented by ‘1‘. The iteration loop will end when all the elements are iterated in the for loop. Therefore, the final count value is nothing but the number of elements in the list.
Get number of unique elements in the list
Sometimes lists can have multiple elements including duplicates. You may be wanted to find the number of elements without duplicates. Let’s see how to find the unique elements count in the given list using Python.
my_third_list = [10, 20, 10, 40, 50, 11, 22, 33, 50, 55, 66, 40] number_of_elements = len(my_third_list) number_of_unique_elements = len(set(my_third_list)) print("Number of elements in the list: ", number_of_elements) print("Number of unique elements in the list: ", number_of_unique_elements)
When you run the above example, you will see the following output.
Number of elements in the list: 12 Number of unique elements in the list: 9
Hence the number of unique elements in the given list is 9.
How to test for an empty or non empty of the list ?
Note, you can also check whether the list if empty or not before you get number of elements in a given list. To check if list is empty or not you can use len() function in the following ways.
if len(my_first_list) > 0: print("List is not empty")
Output
List is not empty
if len(my_first_list): print("list is not empty")
Output
List is not empty
afag
if my_first_list: print("List is not empty")
Output
List is not empty
if not my_second_list: print("List is empty")
Output
List is empty
That’s it. You had learnt how to use len() function and for loop to find the number of elements in the list. Also, learnt how to find unique elements count in the list. Finally, learnt ways to use len() function to test whether the given list is empty or not.
Hope it is helpful π
You’ll also like:
- Convert list of lists to flat list β flatten list in Python ?
- Concatenate or join two lists using Python
- Single digit number to double digits string conversion in Python
- String Immutability with Examples in Python
- Python Program to Check Given Number is Odd or Even
- Increase the cell width of the Jupyter Notebook in browser
- Add python3 kernel to jupyter IPython notebook ?
- Programs to Print Patterns β Pyramid, Triangle using Star
- How to change the default theme in Jupyter Notebook ?
- Change the Jupyter Notebook startup folder in Windows & Mac
- To run Jupyter Notebook on Windows from command line
- Get the first and last element of a list in Python ?
- Difference between Pythonβs list methods append and extend ?
- Python equivalent for && (logical and) to use in if statement
- Split list in to chunks of size n in Python
- How to check if the given list is empty in Python ?
- Remove non-numeric characters from string in Python
- Install Python 3 on Windows 10 machine
- Convert negative to positive number in Python
- Count the number of occurrences of a list item in Python ?
- Extract numbers from a string in python
- How to access index and value in for loop β Python?
- Find index of an element in a list β Python
- Prevent the list that changes unexpected after assignment β Python ?