深度阅读

How to get the length of a List in Python

作者
作者
2023年08月22日
更新时间
6.36 分钟
阅读时间
0
阅读量

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!

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。