
- 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 program to count the pairs of reverse strings
When it is required to count the pairs of reverse strings, a simple iteration is used.
Example
Below is a demonstration of the same
my_list = [{"Python": 8, "is": 1, "fun": 9}, {"Python": 2, "is": 9, "fun": 1}, {"Python": 5, "is": 10,"fun": 7}] print("The list is :") print(my_list) result = {} for dic in my_list: for key, value in dic.items(): if key in result: result[key] = max(result[key], value) else: result[key] = value print("The result is :") print(result)
Output
The list is : [{'Python': 8, 'is': 1, 'fun': 9}, {'Python': 2, 'is': 9, 'fun': 1}, {'Python': 5, 'is': 10, 'fun': 7}] The result is : {'Python': 8, 'is': 10, 'fun': 9}
Explanation
A list of dictionary is defined and is displayed on the console.
An empty dictionary is created.
The elements of the list are iterated over.
The items of the dictionary are iterated over.
If the key is present in the dictionary, then the maximum of key and value is assigned to result.
Otherwise, the value is placed in the result.
This is the result that is displayed on the console.
- Related Articles
- Python program to count Bidirectional Tuple Pairs
- Program to count the number of consistent strings in Python
- Python program to count pairs for consecutive elements
- C Program to Reverse Array of Strings
- Python program to Sort Strings by Punctuation count
- Program to count sorted vowel strings in Python
- Program to count nice pairs in an array in Python
- Python Program to Count number of binary strings without consecutive 1’
- Program to count pairs with XOR in a range in Python
- Program to count number of fraction pairs whose sum is 1 in python
- C++ program to concatenate strings in reverse order
- Count pairs of non-overlapping palindromic sub-strings of the given string in C++
- Program to count number of strings we can make using grammar rules in Python
- Program to count indices pairs for which elements sum is power of 2 in Python
- Javascript Program to Count pairs with given sum

Advertisements