Get number of elements in a list – Python

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

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,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
my_first_list = [10, 20, 30, 40, 50, 11, 22, 33, 44, 55, 66, 77]
my_second_list = []
my_first_list = [10, 20, 30, 40, 50, 11, 22, 33, 44, 55, 66, 77] my_second_list = []
my_first_list = [10, 20, 30, 40, 50, 11, 22, 33, 44, 55, 66, 77]
my_second_list = []

length

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
len(my_first_list)
12
len(my_second_list)
0
len(my_first_list) 12 len(my_second_list) 0
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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))
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))
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Number of elements in the list: 12
Number of elements in the list: 12
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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)
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)
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Number of elements in the list: 12
Number of unique elements in the list: 9
Number of elements in the list: 12 Number of unique elements in the list: 9
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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
if len(my_first_list) > 0:
print("List is not empty")
if len(my_first_list) > 0: print("List is not empty")
if len(my_first_list) > 0:
    print("List is not empty")

Output

List is not empty

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
if len(my_first_list):
print("list is not empty")
if len(my_first_list): print("list is not empty")
if len(my_first_list):
    print("list is not empty")

Output

List is not empty

afag

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
if my_first_list:
print("List is not empty")
if my_first_list: print("List is not empty")
if my_first_list:
    print("List is not empty")

Output

List is not empty

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
if not my_second_list:
print("List is empty")
if not my_second_list: print("List is 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:

References

Subscribe
Notify of
guest


0 Comments
Inline Feedbacks
View all comments