DEV Community

Discussion on: 30 Days of Python 👨‍💻 - Day 4 - Data Types III

Collapse
 
mowat27 profile image
Adrian Mowat • Edited

Hi Arindam,

Another nice post. I thought you might be interested to know you don't need keys() to check if a key is in a dict. This is more idiomatic.

user = {'name': 'Raghav', 'age': 20, 'country': 'India'}
'name' in user
True
'gender' in user
False

Similarly, we often use sets to deduplicate list and provide fast lookups because a set is stored as a hash of values.

names = ['Bob', 'Dave', 'Stacey', 'Bob', 'Mags', 'Stacey']
set(names)
{'Bob', 'Dave', 'Stacey', 'Mags'}
'Mags' in names
True
'Mags' in set(names)
True

The in keyword works on any iterable value... but I'm sure you will come to that soon enough. Enjoy your loops session. It's really cool stuff.

Cheers

Adrian

Collapse
 
arindamdawn profile image
Arindam Dawn

Thanks Adrian for the suggestion.
My intent was to demonstrate that keys() method is the default one. I forgot to mention explicitly.

I am so glad you found it worth a read :)