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