You can use a dict comprehension:
>>> { k:v for k, v in hand.items() if v }
{'m': 1, 'l': 1}
Or, in pre-2.7 Python, the dict
constructor in combination with a generator expression:
>>> dict((k, v) for k, v in hand.iteritems() if v)
{'m': 1, 'l': 1}
More Related Contents:
- Finding the index of an item in a list
- Check if multiple strings exist in another string
- Determine function name from within that function (without using traceback)
- How to manually create a legend
- How to get first element in a list of tuples?
- Python: defining my own operators?
- Character reading from file in Python
- Jupyter Notebook with Python 3.8 – NotImplementedError
- Is it possible to vectorize recursive calculation of a NumPy array where each element depends on the previous one?
- Maximum sum sublist?
- Login credentials not working with Gmail SMTP
- It is more efficient to use if-return-return or if-else-return?
- `xrange(2**100)` -> OverflowError: long int too large to convert to int
- How to create a new database using SQLAlchemy?
- How do I import variable packages in Python like using variable variables ($$) in PHP?
- How can I generate all possible IPs from a list of ip ranges in Python?
- What is the best way to map windows drives using Python?
- ‘Library not loaded: @rpath/libcudart.7.5.dylib’ TensorFlow Error on Mac
- Is there a difference between “pass” and “continue” in a for loop in Python?
- How can I generate a unique ID in Python? [duplicate]
- python logging to database
- How expensive are Python dictionaries to handle?
- Pytorch RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0
- How to write a list to Excel column?
- Two’s Complement Binary in Python?
- Upper case first letter of each word in a phrase
- Python IMAP search using a subject encoded with utf-8
- Delete an uploaded file after downloading it from Flask
- Creating a Menu in Python [closed]
- Pythonic/efficient way to strip whitespace from every Pandas Data frame cell that has a stringlike object in it