Skip to content

Instantly share code, notes, and snippets.

@pythonhacker
Created January 16, 2018 13:28
Show Gist options
  • Save pythonhacker/d17ac14d17b2ce11724ac10d1a5538bc to your computer and use it in GitHub Desktop.
Save pythonhacker/d17ac14d17b2ce11724ac10d1a5538bc to your computer and use it in GitHub Desktop.
Test homogeneity of a Python data structure using a one liner
# For homogenous inputs, no assertion error
def homogeneity(data):
assert(len(dict(map(lambda x: (type(x), 1), data))) == 1)
@pythonhacker
Copy link
Author

There are better versions of this as follows.

  1. using set - assert(len(set(map(type, data))) == 1)
  2. using dict but without a specific value - assert(len({}.fromkeys(map(type, data))) == 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment