# Python – Test Record existence in Dictionary

• Last Updated : 03 Nov, 2022

Sometimes while working with a pool of records, we can have problems in which we need to check the presence of a particular value of a key for existence. This can have applications in many domains such as day-day programming or web development. Let us discuss certain ways in which this task can be performed.

Method #1 : Using any() + generator expression

The combination of the above functions can be used to perform this task. In this, we simply test for all elements using any(), iterated using generator expression.

## Python3

 `# Python3 code to demonstrate working of``# Test Record existence in Dictionary``# Using any() + generator expression` `# initializing list``test_list ``=` `[{``'name'``: ``'Nikhil'``, ``'age'``: ``22``},``             ``{``'name'``: ``'Akshat'``, ``'age'``: ``23``},``             ``{``'name'``: ``'Akash'``, ``'age'``: ``23``}]` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing key and value``test_key ``=` `'name'``test_val ``=` `'Nikhil'` `# Test Record existence in Dictionary``# Using any() + generator expression``res ``=` `any``(sub[test_key] ``=``=` `test_val ``for` `sub ``in` `test_list)` `# printing result``print``(``"Does key value contain in dictionary list : "` `+` `str``(res))`

Output :
The original list is : [{‘name’: ‘Nikhil’, ‘age’: 22}, {‘name’: ‘Akshat’, ‘age’: 23}, {‘name’: ‘Akash’, ‘age’: 23}]
Does key value contain in dictionary list : True

Method #2 : Using filter() + lambda

The combination of the above functions can be used to perform this task. In this, we check for all values using filter and iteration using lambda function.

## Python3

 `# Python3 code to demonstrate working of``# Test Record existence in Dictionary``# Using filter() + lambda` `# initializing list``test_list ``=` `[{``'name'``: ``'Nikhil'``, ``'age'``: ``22``},``             ``{``'name'``: ``'Akshat'``, ``'age'``: ``23``},``             ``{``'name'``: ``'Akash'``, ``'age'``: ``23``}]`  `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing key and value``test_key ``=` `'name'``test_val ``=` `'Nikhil'` `# Test Record existence in Dictionary``# Using filter() + lambda``res ``=` `filter``(``lambda` `sub: test_val ``in` `sub.values(), test_list)``if` `len``(``list``(res)):``    ``res ``=` `True``else``:``    ``res ``=` `False` `# printing result``print``(``"Does key value contain in dictionary list : "` `+` `str``(res))`

Output :
The original list is : [{‘name’: ‘Nikhil’, ‘age’: 22}, {‘name’: ‘Akshat’, ‘age’: 23}, {‘name’: ‘Akash’, ‘age’: 23}]
Does key value contain in dictionary list : True

Method #3 : Using keys() method

## Python3

 `# Python3 code to demonstrate working of``# Test Record existence in Dictionary` `# initializing list``test_list ``=` `[{ ``'name'` `: ``'Nikhil'``, ``'age'` `: ``22``},``            ``{ ``'name'` `: ``'Akshat'``, ``'age'` `: ``23``},``            ``{ ``'name'` `: ``'Akash'``, ``'age'` `: ``23``}]`  `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))` `# initializing key and value``test_key ``=` `'name'``test_val ``=` `'Nikhil'` `# Test Record existence in Dictionary``res``=``False``for` `i ``in` `test_list:``    ``if` `test_key ``in` `i.keys() ``and` `i[test_key]``=``=``test_val:``        ``res``=``True``# printing result``print``(``"Does key value contain in dictionary list : "` `+` `str``(res))`

Output

```The original list is : [{'name': 'Nikhil', 'age': 22}, {'name': 'Akshat', 'age': 23}, {'name': 'Akash', 'age': 23}]
Does key value contain in dictionary list : True```

My Personal Notes arrow_drop_up