
- 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 – Concatenate Tuple to Dictionary Key
When it is required to concatenate tuple to a dictionary key, a list comprehension and the ‘join’ attribute are used.
Example
Below is a demonstration of the same −
my_list = [(("pyt", "is", "best"), 10), (("pyt", "cool"), 1), (("pyt", "is", "fun"), 15)] print("The list is :") print(my_list) my_result = {} for sub_list in my_list: my_result[" ".join(sub_list[0])] = sub_list[1] print("The result is :") print(my_result)
Output
The list is : [(('pyt', 'is', 'best'), 10), (('pyt', 'cool'), 1), (('pyt', 'is', 'fun'), 15)] The result is : {'pyt is best': 10, 'pyt cool': 1, 'pyt is fun': 15}
Explanation
A list of tuple is defined and is displayed on the console.
An empty dictionary is created.
The list is iterated over, and the list comprehension is used to remove spaces.
This is the output that is displayed on the console.
- Related Articles
- Convert Nested Tuple to Custom Key Dictionary in Python
- Add dictionary to tuple in Python
- Python – Concatenate Rear elements in Tuple List
- Dictionary to list of tuple conversion in Python
- Convert tuple to adjacent pair dictionary in Python
- How to truncate Key Length in Python Dictionary?
- How to search Python dictionary for matching key?
- Python - Ways to remove a key from dictionary
- List vs tuple vs dictionary in Python
- How to concatenate a Python dictionary to display all the values together?
- How to remove a key from a python dictionary?
- Add a key value pair to dictionary in Python
- Convert key-values list to flat dictionary in Python
- Accessing Key-value in a Python Dictionary
- Python program to get maximum of each key Dictionary List

Advertisements