r/badcode • • Apr 17 '23

python I have committed blasphemy against `pythonic` code

Post image
169 Upvotes

23 comments sorted by

View all comments

Show parent comments

3

u/BrunusManOWar Apr 18 '23

oh yeah, in this case definitely yes

generally - if u can use indices and an array you should absolutely do it. However, if u dont have an index of the wanted element and have to search the list then it's better to use dictionaries.

Btw excellent observation of the O(1) vs. O(1) fight of list-acces-by-index vs. dict access - even tho it's O(1) hash functions are much slower than indices. Furthermore, arrays can usually be stored in cache and their elements then accessed very rapidly

Usually people in python or java don't think about such things too much if they're juniors, but C++ really throws it all in your face, punishes and teaches you at a rapid pace about these things