DEV Community

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

Arindam Dawn on June 24, 2020

As I am sharing my daily learning python learning progress, it is becoming more clear and evident to me that learning and sharing explaining concep...
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 :)

Collapse
 
mantasbacys profile image
mantasbacys

Good tutorial. Strangely though list(range(10)) does not seem to work on JupyterLab, but works seamlessly on Jupyter Notebook, it unfortunately took a while to figure this out... I assume you are coding in Jupyter Notebook? Also, for List Unpacking, lists with emojis did not work for me at all, was it supposed to?


TypeError Traceback (most recent call last)
in
7 print(merge_avengers) # ironman spiderman antman hulk
8
----> 9 range_of_numbers = list(range(10)) # quickly generates a list of specific range
10 print(range_of_numbers) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
11 another_range = list(range(0,5)) # with start stop

TypeError: 'list' object is not callable

Collapse
 
arindamdawn profile image
Arindam Dawn

I am not using Jupyter Notebooks. I am using REPL to test my code.
Pardon me, but I was not able to understand the error message properly.
Can you try running the code in REPL or any python code editor?

Collapse
 
kobbyknight profile image
KobbyKnight
list(range[10])

returns list index out of range. Does not generate any list for me.

list(range(0,5))

this does not work either. Returns list object is not callable...

Collapse
 
arindamdawn profile image
Arindam Dawn

Thank you for pointing that out. There was a typo in my post. I have rectified it now.

The first one should be list(range(10))

The second one should work fine. Where are you trying to evaluate this?
Generally, list index out of range occurs if we try to access an element in a list which is not present.

Collapse
 
sunny521012 profile image
sunny521012

Yes it works

Collapse
 
szmatyifa profile image
Matt

HI Arindam!

at the part of

user = {'name': 'Max', 'age': 40, 'married': False}
print(user['name']) # Max
print(user['married'] # False

u missed an ')' after ['married']

And it took me half an hour to figure 😛

Collapse
 
szmatyifa profile image
Matt • Edited

HI Arindam!

It s me again.
I found another missing ')' , this time i was faster.

at the part of

print(abstract['first'] # 123
print(abstract[True]) # 'hello
print(abstract[777]) # [1,3,4,5]

after ['first']
😝

Collapse
 
arindamdawn profile image
Arindam Dawn

Thanks Matt for taking the time to report this. Yeah sometimes it's the most silly things that we tend to ignore!