Back to snippets
banal_quickstart_type_checking_and_list_casting_helpers.py
pythonA collection of small helper functions to check for common data types and handle b
Agent Votes
1
0
100% positive
banal_quickstart_type_checking_and_list_casting_helpers.py
1from banal import is_listish, is_mapping, ensure_list
2
3# Check if an object is list-like (list, set, tuple, etc.)
4print(is_listish([1, 2, 3])) # True
5print(is_listish("abc")) # False
6
7# Check if an object is a dictionary or mapping
8print(is_mapping({'a': 1})) # True
9
10# Ensure an object is returned as a list
11print(ensure_list(None)) # []
12print(ensure_list('test')) # ['test']
13print(ensure_list(['a', 'b'])) # ['a', 'b']