How to get the length of a List in Python

Published on Aug. 22, 2023, 12:15 p.m.

To get the length of a List in Python, you can use the built-in len() function. Here’s an example:

my_list = ['apple', 'banana', 'cherry']
print(len(my_list))

This will output 3, which is the number of elements in the list.

You can also use a for loop to iterate over the list and count the elements like this:

my_list = ['apple', 'banana', 'cherry']
count = 0
for element in my_list:
    count += 1
print(count)

This will also output 3.

Note that the len() function works for other data types as well, such as strings, dictionaries, and tuples. I hope this helps you find the size of a list in Python!

Tags:

related content