深度阅读

How to create a list of dictionary keys in python

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

There are multiple ways to create a list of dictionary keys in Python. Here are a few examples:

  1. Using the “keys” method:
my_dict = {'a': 1, 'b': 2, 'c': 3}
key_list = list(my_dict.keys())
print(key_list)  # Output: ['a', 'b', 'c']
  1. Using the dictionary itself inside “list” method:
my_dict = {'a': 1, 'b': 2, 'c': 3}
key_list = list(my_dict)
print(key_list)  # Output: ['a', 'b', 'c']
  1. Using list comprehension:
my_dict = {'a': 1, 'b': 2, 'c': 3}
key_list = [key for key in my_dict]
print(key_list)  # Output: ['a', 'b', 'c']

No matter which method you use all of them return a list of dictionary keys.

相关标签

博客作者

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