

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Dictionaries with Unique Value Lists
When it is required to get the dictionaries with unique value lists, the ‘set’ operator and the list methods are used, along with a simple iteration.
Example
Below is a demonstration of the same −
my_dictionary = [{'Python' : 11, 'is' : 22}, {'fun' : 11, 'to' : 33}, {'learn' : 22},{'object':9},{'oriented':11}] print("The dictionary is : " ) print(my_dictionary) my_result = list(set(value for element in my_dictionary for value in element.values())) print("The resultant list is : ") print(my_result) print("The resultant list after sorting is : ") my_result.sort() print(my_result)
Output
The dictionary is : [{'Python': 11, 'is': 22}, {'fun': 11, 'to': 33}, {'learn': 22}, {'object': 9}, {'oriented': 11}] The resultant list is : [33, 11, 22, 9] The resultant list after sorting is : [9, 11, 22, 33]
Explanation
A list of dictionary is defined and is displayed on the console.
The values in the dictionary are accessed by iterating over, and converted to a set.
This way, the unique elements are obtained.
It is then converted to a list, and assigned to a variable.
It is displayed as the output on the console.
It is again sorted and displayed on the console.
- Related Questions & Answers
- Sort Python Dictionaries by Key or Value
- Python Program to get all unique keys from a List of Dictionaries
- Python – Remove Dictionaries with Matching Values
- Python – Filter dictionaries with ordered values
- How to perform Calculations with Dictionaries in Python?
- Python – Merge Dictionaries List with duplicate Keys
- Python – Cross mapping of Two dictionary value lists
- Python – Extract dictionaries with values sum greater than K
- Assign value to unique number in list in Python
- Python - Unique keys count for Value in Tuple List
- Python program to merge to dictionaries.
- Handling missing keys in Python dictionaries
- How to merge multiple Python dictionaries?
- What are Ordered dictionaries in Python?
- get() method for dictionaries in Python
Advertisements