Convert Dictionary keys to a List

Convert Dictionary keys to a List in Python

In Python, dictionaries are useful data structures that use keys for indexing. They are an unordered sequence of items (key-value pairs), so the order does not matter. The keys are not able to be changed. Dictionaries, like lists, can hold a variety of data types, including integers, floats, strings, NaN, Booleans, lists, arrays, and even nested dictionaries.

Given a dictionary ,the task is to convert keys of dictionary to a list.

Examples:

Input:

dictionary = {'Hello': 600, 'world': 300, 'BTechGeeks': 900}

Output:

list of keys :  ['Hello', 'world', 'BTechGeeks']

Convert Dictionary keys to a List

There are several ways to convert dictionary keys to a list some of them are:

Method #1:Using list()

A dictionary object is also an iterable object in Python, allowing you to iterate over all of the dictionary’s keys.

In Python, there is a member function called dict.keys that is part of the dictionary class ()

It returns a view object or an iterator that loops through the dictionary’s keys. This object can be used to iterate over existing lists or to create new ones.

Below is the implementation:

# Given dictionary
dictionary = {'Hello': 600, 'world': 300, 'BTechGeeks': 900}
# convert given_dictionary keys  to list
list_of_keys = list(dictionary.keys())
# print the converted list
print("list of keys : ", list_of_keys)

Output:

list of keys :  ['Hello', 'world', 'BTechGeeks']

Method #2:Using List Comprehension

The dictionary class in Python has a function keys() that returns an iterable sequence (dict keys) of all keys in the dictionary. Using the sequence returned by keys(), we can use the list comprehension to iterate over all keysin the dictionary and create a list of keys.

Below is the implementation:

# Given dictionary
dictionary = {'Hello': 600, 'world': 300, 'BTechGeeks': 900}
# convert given_dictionary keys  to list using list comprehension
list_of_keys = [key for key in dictionary.keys()]
# print the converted list
print("list of keys : ", list_of_keys)

Output:

list of keys :  ['Hello', 'world', 'BTechGeeks']

Method #3 :Using unpacking

Unpacking * works for any iterable object, so it is simple to create a list by using it in a list literal because dictionaries return their keys when iterated through.

Below is the implementation:

# Given dictionary
dictionary = {'Hello': 600, 'world': 300, 'BTechGeeks': 900}
# convert given_dictionary keys  to list using args function
list_of_keys = [*dictionary]
# print the converted list
print("list of keys : ", list_of_keys)

Output:

list of keys :  ['Hello', 'world', 'BTechGeeks']

Method #4:Converting only specified keys to list

Assume we want to convert only a subset of the keys in a dictionary to a list. For example, create a list of only the keys in the dictionary with values greater than 500. To accomplish this, we can use an if condition to iterate over pairs of dictionary keys and select only those keys where the condition returns True.

Below is the implementation:

# Given dictionary
dictionary = {'Hello': 600, 'world': 300, 'BTechGeeks': 900}
# convert specified given_dictionary keys to list
list_of_keys = [key for key in dictionary.keys() if dictionary[key] > 500]
# print the converted list
print("list of keys : ", list_of_keys)

Output:

list of keys :  ['Hello', 'BTechGeeks']

 
Related Programs: