tags:

views:

92

answers:

2

I seem to be using this block of code alot in Python.

if Y is not None:
    obj[X][0]=Y

How do I establish a mapping from X=>Y and then iterate through this entire mapping while calling that block of code on X and Y

A: 

If Y is None, you can do something like:

default_value = 0
obj[X][0] = Y if not None else default_value
Casey
.. = Y if Y not None else default_value
kaizer.se
If Y should never be a false value, you use a much shorter pythonic alternative: `... = Y or default_value`
kaizer.se
+5  A: 
mapping = {X1: Y1, X2: Y2, X3: Y3}
mapping[X4] = Y4
mapping[X5] = Y5

for X,Y in mapping.items():
    if Y is not None:
        obj[X][0] = Y
John Kugelman