/  Technology   /  How to return dictionary keys as a list in Python?
How to return dictionary keys as a list in Python

How to return dictionary keys as a list in Python?

 

Dictionary is an unordered collection of data in the form of key-value pairs separated by commas inside curly brackets.

 

Unlike sequences, which are iterables that support accessing of elements using integer indexing, dictionaries are indexed by keys. Dictionaries are generally optimized to retrieve values when the key is known. Values (definitions) mapped to a specific key (words) are similar to that of a dictionary in the real world.

 

  • Using dict.keys()
  • Using manual method
  • By Type Casting
  • By Unpacking with *
  • Using itemgetter

 

Using dict.keys():

The dictionary method keys() returns all the keys present in a dictionary in the form of a list.

Example:

demo_dict = {1:'One', 2:'Two', 3;'Three'}

list_to_keys= demo_dict.keys()
print(list_to_keys)

Output:

 

Using manual method:

We take an empty list named demo_list and using a for loop, we iterate through each element in the list containing only the keys of the dictionary which is created using the dict.keys() function. We now add each of these keys to the empty list using the dict.append() method. We put the complete code into a function named list_of_keys and pass the demo_dict as a formal argument.

Example:

def list_of_keys(demo_dict):
   demo_list = []
   for key in demo_dict.keys():
      demo_list.append(key)

   return demo_list

demo_dict = {1:'One', 2:'Two', 3:'Three'}
print("The list of keys are: "+ str(list_of_keys(demo_dict)))

Output:

 

By Type Casting:

We first get all the keys of the dictionary using the dictionary method dict.keys() and then typecast it using the list() function.

Example:

def list_of_keys(demo_dict):
   return list(demo_dict.keys())

demo_dict = {1:'One', 2:'Two', 3:'Three'}
print("The list of keys are: "+ str(list_of_keys(demo_dict)))

Output:

 

By Unpacking with *:

This method works with any object which is an iterable and as dictionaries return their keys when they are iterated through, we can create a list of keys.

Example:

def list_of_keys(demo_dict):
   return [*demo_dict]

demo_dict = {1:'One', 2:'Two', 3:'Three'}
print("The list of keys are: "+ str(list_of_keys(demo_dict)))

Output:

 

Using itemgetter:

The itemgetter() method is one of the many methods present in the operator module. It takes in one or more integers as argument(s) and returns a callable object. This object acts like a special function and can access elements using indexing.

Example:

from operator import itemgetter

def list_of_keys(demo_dict):
   return list(map(itemgetter(0), demo_dict.item()))

demo_dict = {1:'One', 2:'Two', 3:'Three'}
print("The list of keys are: "+ str(list_of_keys(demo_dict)))

Output:

 

Here the callable object fetches an item from its operand using the operand’s __getitem__() method. This method is then mapped to demo_dict.items() and then typecasted to list using the list() function.

 

Leave a comment