
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Limit the values to keys in a Dictionary List
When it is required to limit the values to keys in a list of dictionary, the keys are accessed and the ‘min’ and ‘max’ methods are used to limit the values.
Example
Below is a demonstration of the same
my_list = [{"python": 4, "is": 7, "best": 10},{"python": 2, "is": 5, "best": 9},{"python": 1, "is": 2, "best": 6}] print("The list is :") print(my_list) my_result = dict() keys = list(my_list[0].keys()) for my_elem in keys: my_result[my_elem] = [min(sub[my_elem] for sub in my_list), max(sub[my_elem] for sub in my_list)] print("The result is :") print(my_result)
Output
The list is : [{'python': 4, 'is': 7, 'best': 10}, {'python': 2, 'is': 5, 'best': 9}, {'python': 1, 'is': 2, 'best': 6}] The result is : {'python': [1, 4], 'is': [2, 7], 'best': [6, 10]}
Explanation
A list of dictionary is defined and is displayed on the console.
An empty dictionary is created.
The keys of the list of dictionary are converted into a list and stored in a variable.
These values are iterated over and the minimum and maximum of values are obtained.
This is done by iterating over the elements in the list.
This is displayed as output on the console.
- Related Articles
- How to create Python dictionary from list of keys and values?
- Get dictionary keys as a list in Python
- Keys associated with Values in Dictionary in Python
- Append Dictionary Keys and Values (In order ) in dictionary using Python
- How to convert Python dictionary keys/values to lowercase?
- How to insert new keys/values into Python dictionary?
- How to Common keys in list and dictionary using Python
- Find keys with duplicate values in dictionary in Python
- How to get a list of all the keys from a Python dictionary?
- How to access nested Python dictionary items via a list of keys?
- Python – Inverse Dictionary Values List
- How to split Python dictionary into multiple keys, dividing the values equally?
- Python - Combine two dictionary adding values for common keys
- C# program to get the List of keys from a Dictionary
- Convert key-values list to flat dictionary in Python

Advertisements