深度阅读

How to get the length of a dictionary in Python?

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

To get the length (i.e., the number of key-value pairs) of a Python dictionary, you can use the built-in len() function. Here’s an example:

my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
dict_length = len(my_dict)
print(dict_length)  # Output: 3

In this example, the len() function is used to get the length of the dictionary. The resulting number is stored in the dict_length variable and printed to the console.

Note that the length function in the search results example is different, as it returns the length of the dictionary when it’s treated as a list of its keys.

相关标签

博客作者

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