Python | Test if key exists in tuple keys dictionary

Sometimes, while working with dictionary data, we need to check if a particular key is present in dictionary. If keys are elementary, the solution of problem in discussed and easier to solve. But sometimes, we can have tuple as key of dictionary. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using `any()` + generator expression

Combination of above functionalities can be used to perform this task. In this, we check for each element inside each key for the target key. The any() is used to check in any keys of dictionary.

 `# Python3 code to demonstrate working of ` `# Test if key exists in tuple keys dictionary ` `# using any() + generator expression ` ` `  `# initialize dictionary ` `test_dict ``=` `{(``4``, ``5``) : ``'1'``, (``8``, ``9``) : ``'2'``, (``10``, ``11``) : ``'3'``} ` ` `  `# printing original dictionary ` `print``(``"The original dictionary : "` `+` `str``(test_dict)) ` ` `  `# Test key  ` `key ``=` `10` ` `  `# Test if key exists in tuple keys dictionary ` `# using any() + generator expression ` `res ``=` `any``(key ``in` `sub ``for` `sub ``in` `test_dict) ` ` `  `# printing result ` `print``(``"Does key exists in dictionary? : "` `+` `str``(res)) `

Output :

```The original dictionary : {(4, 5): '1', (8, 9): '2', (10, 11): '3'}
Does key exists in dictionary? : True
```

Method #2 : Using `from_iterable()`

This task can also be performed using this function. In this, we flatten the keys and then check for existence.

 `# Python3 code to demonstrate working of ` `# Test if key exists in tuple keys dictionary ` `# using from_iterable() ` `from` `itertools ``import` `chain ` ` `  `# initialize dictionary ` `test_dict ``=` `{(``4``, ``5``) : ``'1'``, (``8``, ``9``) : ``'2'``, (``10``, ``11``) : ``'3'``} ` ` `  `# printing original dictionary ` `print``(``"The original dictionary : "` `+` `str``(test_dict)) ` ` `  `# Test key  ` `key ``=` `10` ` `  `# Test if key exists in tuple keys dictionary ` `# using from_iterable() ` `res ``=` `key ``in` `chain.from_iterable(test_dict) ` ` `  `# printing result ` `print``(``"Does key exists in dictionary? : "` `+` `str``(res)) `

Output :

```The original dictionary : {(4, 5): '1', (8, 9): '2', (10, 11): '3'}
Does key exists in dictionary? : True
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.