python · March 25, 2023 0

How to get the length of a List in Python

Table of Content

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']

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

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!

%d bloggers like this: