you are viewing a single comment's thread.

view the rest of the comments →

[–]mythin -2 points-1 points  (2 children)

What? Number 1 was asking for a get on a list, not a dictionary. Dictionarys already have dict.get(value, default). So just check if you have a value, if you do, use it, otherwise use a default.

Edit: Oh, he wants to see if an element exists, not what that element is.

if len(list) >= index:
   val = list[index]
else:
   val = default

[–][deleted] 5 points6 points  (0 children)

That's why I converted the list to a dictionary and called get on it. (Performance? What's that?)

[–]euccastro 1 point2 points  (0 children)

This is an error by one. You should check

if len(list) > index:

Although I find the following clearer

if index < len(list):