Posts

Showing posts from July, 2019

Different ways to test multiple flags at once in Python

# Different ways to test multiple # flags at once in Python x, y, z = 0 , 1 , 0 if x == 1 or y == 1 or z == 1 : print ( 'passed' ) if 1 in (x, y, z): print ( 'passed' ) # These only test for truthiness: if x or y or z: print ( 'passed' ) if any ((x, y, z)): print ( 'passed' )

Merging two dicts

# How to merge two dictionaries # in Python 3.5+ >>> x = { 'a' : 1 , 'b' : 2 } >>> y = { 'b' : 3 , 'c' : 4 } >>> z = { ** x, ** y} >>> z {'c': 4, 'a': 1, 'b': 3} # In Python 2.x you could # use this: >>> z = dict (x, ** y) >>> z {'a': 1, 'c': 4, 'b': 3} # In these examples, Python merges dictionary keys # in the order listed in the expression, overwriting # duplicates from left to right. # # See: https://www.youtube.com/watch?v=Duexw08KaC8 This email is part of the Python Tricks series where you're getting a new Python trick every few days directly to your inbox. Enjoy! — Dan Bader ( realpython.com )