Get all Tuple Keys from Dictionary - Python
Last Updated :
05 Feb, 2025
In Python, dictionaries can have tuples as keys which is useful when we need to store grouped values as a single key. Suppose we have a dictionary where the keys are tuples and we need to extract all the individual elements from these tuple keys into a list. For example, consider the dictionary : d = {(5, 6): 'gfg', (9, 10): 'best', (1, 2, 8): 'is'} here the keys are (5, 6), (9, 10), and (1, 2, 8) and our task is to retrieve all the elements from these tuples into a single list: [5, 6, 9, 10, 1, 2, 8].
Using dictionary comprehension and chain.from_iterable()
In this method we can use itertools.chain.from_iterable() to flatten the tuple keys directly into a list.
Python
from itertools import chain
d = {(5, 6): 'gfg', (9, 10): 'best', (1, 2, 8): 'is'}
res = list(chain.from_iterable(d.keys()))
print(res)
Output[5, 6, 9, 10, 1, 2, 8]
Explanation:
- d.keys() gives us all tuple keys and chain.from_iterable(d.keys()) flattens the tuple keys into a single sequence.
- list() converts the sequence into a list.
Using a generator expression and chain()
This method offers a memory-efficient way of flattening the tuple keys as the generator expression generates the keys on the fly and chain() combines them into a single list without the need to create multiple intermediate lists.
Python
from itertools import chain
d = {(5, 6): 'gfg', (9, 10): 'best', (1, 2, 8): 'is'}
res = list(chain(*(key for key in d)))
print(res)
Output[5, 6, 9, 10, 1, 2, 8]
Explanation:
- We use a generator expression (key for key in d) to iterate over the tuple keys and the * operator unpacks the tuples before passing them to chain() which flattens them.
- list() converts the result into a list.
Using a loop and extend()
This method is simple and intuitive as it uses a loop to go through each tuple key in the dictionary and extend() function to add the elements of each tuple to a list . while it’s easy to understand it involves more overhead due to the multiple function calls within the loop.
Python
from itertools import chain
d = {(5, 6): 'gfg', (9, 10): 'best', (1, 2, 8): 'is'}
res = []
for key in d:
res.extend(key)
print(res)
Output[5, 6, 9, 10, 1, 2, 8]
Explanation:
- We initialize an empty list res and iterate through each tuple key in the dictionary.
- Using extend(), we add all elements of each tuple to res.
Using sum() with list unpacking
This method is straightforward and simple to understand as it uses sum() to combine the elements of the tuples into one list but it’s less efficient for large datasets because it repeatedly creates new lists for each tuple.
Python
d = {(5, 6): 'gfg', (9, 10): 'best', (1, 2, 8): 'is'}
res = sum((list(key) for key in d), [])
print(res)
Output[5, 6, 9, 10, 1, 2, 8]
Explanation:
- We use a generator expression (list(key) for key in d) to convert each tuple key into a list.
- sum() then adds all these lists together and by providing an initial empty list [] we concatenate all the lists into one.
Similar Reads
Python | Filter Tuple Dictionary Keys Sometimes, while working with Python dictionaries, we can have itâs keys in form of tuples. A tuple can have many elements in it and sometimes, it can be essential to get them. If they are a part of a dictionary keys and we desire to get filtered tuple key elements, we need to perform certain functi
4 min read
Get List of Values From Dictionary - Python We are given a dictionary and our task is to extract all the values from it and store them in a list. For example, if the dictionary is d = {'a': 1, 'b': 2, 'c': 3}, then the output would be [1, 2, 3].Using dict.values()We can use dict.values() along with the list() function to get the list. Here, t
2 min read
Get Total Keys in Dictionary - Python We are given a dictionary and our task is to count the total number of keys in it. For example, consider the dictionary: data = {"a": 1, "b": 2, "c": 3, "d": 4} then the output will be 4 as the total number of keys in this dictionary is 4.Using len() with dictThe simplest way to count the total numb
2 min read
Check if Tuple Exists as Dictionary Key - Python The task is to check if a tuple exists as a key in a dictionary. In Python, dictionaries use hash tables which provide an efficient way to check for the presence of a key. The goal is to verify if a given tuple is present as a key in the dictionary.For example, given a dictionary d = {(3, 4): 'gfg',
3 min read
Python - Remove Disjoint Tuple Keys from Dictionary We are given a dictionary we need to remove the Disjoint Tuple key from it. For example we are given a dictionary d = {('a', 'b'): 1, ('c',): 2, ('d', 'e'): 3, 'f': 4} we need to remove all the disjoint tuple so that the output should be { }. We can use multiple methods like dictionary comprehension
3 min read
Python - Iterate over Tuples in Dictionary In this article, we will discuss how to Iterate over Tuples in Dictionary in Python. Method 1: Using index We can get the particular tuples by using an index: Syntax: dictionary_name[index] To iterate the entire tuple values in a particular index for i in range(0, len(dictionary_name[index])): print
2 min read