Created
January 16, 2018 13:28
-
-
Save pythonhacker/d17ac14d17b2ce11724ac10d1a5538bc to your computer and use it in GitHub Desktop.
Test homogeneity of a Python data structure using a one liner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For homogenous inputs, no assertion error | |
def homogeneity(data): | |
assert(len(dict(map(lambda x: (type(x), 1), data))) == 1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are better versions of this as follows.
assert(len(set(map(type, data))) == 1)
assert(len({}.fromkeys(map(type, data))) == 1)