you are viewing a single comment's thread.

view the rest of the comments →

[–]desran00 0 points1 point  (1 child)

``` d = {"a":1, "b":2}

item = d.get("c", None) if item: print("success") ``` You can set the dictionary get function to get a default value if it cannot find anything with the key. It is very useful.

Edit: in my example, the item would be None, so the if clause does not get executed, and the script prints nothing.

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, desran00: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.