mikeckennedy/write-pythonic-code-demos

Index error in _01_perf.py

Closed this issue · 1 comments

If I'm not mistaken,

    interesting_ids = {random.randint(0, len(data_list)) for _ in range(0, 100)}

should be

    interesting_ids = {random.randint(0, len(data_list)-1) for _ in range(0, 100)}

Or follow the tip from a previous lesson, and use random.choice().

Thanks @natter1 That is probably true. I don't think I mean to allow for None to be returned (although I don't think it'd crash). I updated the code to use -1.

I do prefer random.choice() too. But in this case, it defeats the purpose. It returns things like:

[DataPoint(id=246359, x=669, y=312, temp=14, quality=0.5351748892355757), 
DataPoint(id=19359, x=584, y=429, temp=37, quality=0.41538909477667063), 
...]

The goal of this exercise was to see that doing lookups on lists to get that data is a bad idea but doing lookups on dictionaries and sets is incredibly fast.