
- 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 unique keys count for Value in Tuple List
When it is required to get the count of unique keys for values in a list of tuple, it can be iterated over and the respective counts can be determined.
Below is a demonstration of the same −
Example
import collections my_result = collections.defaultdict(int) my_list = [[('Hello', 'Hey')], [('Jane', 'Will')], [('William', 'John')], [('Hello', 'Hey')], [('z', 'q')]] print("The list of list is :") print(my_list) for elem in my_list: my_result[elem[0]] += 1 print("The result is : ") print(my_result)
Output
The list of list is : [[('Hello', 'Hey')], [('Jane', 'Will')], [('William', 'John')], [('Hello', 'Hey')], [('z', 'q')]] The result is : defaultdict(<class 'int'>, {('Hello', 'Hey'): 2, ('Jane', 'Will'): 1, ('William', 'John'): 1, ('z', 'q'): 1})
Explanation
The required packages are imported.
A list of list of tuples is defined, that contains string and characters.
The list is displayed on the console.
The list is iterated over, and the first element is incremented by 1.
This result is displayed on the console.
- Related Articles
- Python - Unique keys count for Value in Tuple List
- Python Program to get all unique keys from a List of Dictionaries
- Count unique sublists within list in Python
- Python program to count Bidirectional Tuple Pairs
- Assign value to unique number in list in Python
- Python program to Flatten Nested List to Tuple List
- Python program to count the elements in a list until an element is a Tuple?
- Python program to print the keys and values of the tuple
- Assign ids to each unique value in a Python list
- Flatten tuple of List to tuple in Python
- Python program to count occurrences of an element in a tuple
- Maximum value in record list as tuple attribute in Python
- Finding unique elements from Tuple in Python
- Count occurrence of all elements of list in a tuple in Python
- Python Program to print unique values from a list

Advertisements