In this article we will discuss how to create a list of all keys in a dictionary.

Suppose we have a dictionary of string and integers i.e.

# Dictionary of string and int
wordFreqDic = {
    "Hello": 56,
    "at" : 23 ,
    "test" : 43,
    "this" : 78,
    "here" : 18,
    "city" : 2,
    }

Now how to get all the keys from above dictionary in a list  i.e.
['city', 'test', 'at', 'Hello', 'this', 'here']

Creating a list of all keys in dictionary using dict.keys()

In python, dictionary class provides a member function i.e.

dict.keys()

It returns a view object or iterator to the list of all keys in dictionary.  We can use this object for iteration or creating new list. Let’s use that to get the list of all keys in the above dictionary.
# Create a new list from the view object returned by keys() 
dictkeys = list (wordFreqDic.keys())

dictkeys content will be,
['city', 'test', 'at', 'Hello', 'this', 'here']

Creating a filtered list of dictionary keys using List Comprehension

Suppose from above mentioned dictionary, we want a list of keys that start with character ‘t’ only i.e.

['test', 'this']

let’s do that using for loop i.e.
dictkeys = list()

# Creating a list of keys that start with 't'     
for x in wordFreqDic :
    if x.startswith('t') :
        dictkeys.append(x)

dictkeys content will be,
['test', 'this']

But that’s not pythonic. Let’s do that using list comprehension,
# Creating a list of keys that start with 't'        
dictkeys = [x for x in wordFreqDic if x.startswith('t')]

dictkeys content will be,
['test', 'this']

Python Dictionary Tutorial - Series:

  1. What is a Dictionary in Python & why do we need it?
  2. Creating Dictionaries in Python
  3. Iterating over dictionaries
  4. Check if a key exists in dictionary
  5. Check if a value exists in dictionary
  6. Get all the keys in Dictionary
  7. Get all the Values in a Dictionary
  8. Remove a key from Dictionary
  9. Add key/value pairs in Dictionary
  10. Find keys by value in Dictionary
  11. Filter a dictionary by conditions
  12. Print dictionary line by line
  13. Convert a list to dictionary
  14. Sort a Dictionary by key
  15. Sort a dictionary by value in descending or ascending order
  16. Dictionary: Shallow vs Deep Copy
  17. Remove keys while Iterating
  18. Get all keys with maximum value
  19. Merge two or more dictionaries in python

Subscribe with us to join a list of 2000+ programmers and get latest tips & tutorials at your inbox through our weekly newsletter.

Complete example is as follows,

def main():
    
    # Dictionary of string and int
    wordFreqDic = {
        "Hello": 56,
        "at" : 23 ,
        "test" : 43,
        "this" : 78,
        "here" : 18,
        "city" : 2,
        }

    print("Dictionary : ", wordFreqDic)
    
    '''
    Creating a list of keys in dictionary
    '''
    
    # Create a new list from the view object returned by keys() 
    dictkeys = list (wordFreqDic.keys())
    
    print("List of keys in Dictionary : ", dictkeys)
    
    
    '''
    Creating a filtered list of keys in dictionary using for loop
    '''
    
    dictkeys = list()
    
    # Creating a list of keys that start with 't'     
    for x in wordFreqDic :
        if x.startswith('t') :
            dictkeys.append(x)
     
    print("List of keys in Dictionary that start with 't' : " , dictkeys)
    
    '''
    Creating a filtered list of keys in dictionary using List comprehension
    '''
    
    # Creating a list of keys that start with 't'        
    dictkeys = [x for x in wordFreqDic if x.startswith('t')]
    
    print("List of keys in Dictionary that start with 't' : " , dictkeys)

    
if __name__ == '__main__':
    main()

Output:
Dictionary :  {'city': 2, 'test': 43, 'at': 23, 'Hello': 56, 'this': 78, 'here': 18}
List of keys in Dictionary :  ['city', 'test', 'at', 'Hello', 'this', 'here']
List of keys in Dictionary that start with 't' :  ['test', 'this']
List of keys in Dictionary that start with 't' :  ['test', 'this']

 

Join a list of 2000+ Programmers for latest Tips & Tutorials