Let's take a look at some of the possible ways in Python Programming to return a dictionary (dict) keys as a list.
Example 1: Using the dict.keys() method
locations = {'USA': 'New York', 'Japan': 'Tokyo', 'India': 'Mumbai'}
country_list = list(locations.keys())
print(country_list)
Output:
['USA', 'Japan', 'India']
Example 2: Using dictionary unpacking
locations = {'USA': 'New York', 'Japan': 'Tokyo', 'India': 'Mumbai'}
country_list = [*locations]
print(country_list)
Example 3: Using list comprehension
locations = {'USA': 'New York', 'Japan': 'Tokyo', 'India': 'Mumbai'}
country_list = [country for country in locations]
print(country_list)

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!