Python : How to create a list of all the Values in a dictionary ?

In this article we will discuss how to create a list of all the values in 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" : 2,
    "here" : 23,
    "city" : 2,
    }

Now we want all the Values from above dictionary in a list i.e.

[2, 43, 2, 23, 56, 23]

Let’s see how to do that,

Advertisements

Create a list of all values in dict using dict.values()

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

dict.values()

It returns a view object / iterator to the list of all values in dictionary.  We can use this object for iteration or creating new list.

Read More  How to slice a pandas DataFrame column?

Let’s use that to get the list of all values in the above dictionary.

# Create a new list from the view object returned by values() 
dictValues = list (wordFreqDic.values())

dictValues content will be,

[2, 43, 2, 23, 56, 23]

Creating a list of duplicate values in dictionary

Suppose instead of all values we want a list of all duplicate values from the dictionary i.e.

Read More  How to use "not equal" operator in Python?

[23 , 2]

Let’s do that using for loop i.e.

'''
Creating a list of dulicate values in dictionary
'''
uniqueValues = list()
duplicateValues = list()

# Creating a list of all duplicate values in dictionary     
for x in wordFreqDic.values() :
    if x not in uniqueValues :
        uniqueValues.append(x)
    else:    
        duplicateValues.append(x)
 
print("List of Duplicate values in Dictionary" , duplicateValues)

dictkeys content will be,

Read More  Python : How to Insert an element at specific index in List ?
[2, 23]

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" : 2,
        "here" : 23,
        "city" : 2,
        }

    print(wordFreqDic)

    '''
    Creating a list of all values in dictionary
    '''
    
    # Create a new list from the view object returned by values() 
    dictValues = list (wordFreqDic.values())
    
    print("List of values in Dictionary : ", dictValues)
    
    '''
    Creating a list of dulicate values in dictionary
    '''
    uniqueValues = list()
    duplicateValues = list()
    
    # Creating a list of all duplicate values in dictionary     
    for x in wordFreqDic.values() :
        if x not in uniqueValues :
            uniqueValues.append(x)
        else:    
            duplicateValues.append(x)
     
    print("List of Duplicate values in Dictionary" , duplicateValues)
    
   
    
    
if __name__ == '__main__':
    main()

Output:

{'city': 2, 'test': 43, 'this': 2, 'at': 23, 'Hello': 56, 'here': 23}
List of values in Dictionary :  [2, 43, 2, 23, 56, 23]
List of Duplicate values in Dictionary [2, 23]

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top