In python (especially python 3) comprehensions can be extremely powerful tools. They allow you to in very concise code do pretty complex data manipulation, creating sets, dictionaries or lists. When coupled with a generator to produce data, they are pretty quick too, and tend to be somewhat more readable than nested map/filter operations.

There are 4 types of comprehension in python:

List Comprehension:

[x for x in foo if x not in bar]

Set Comprehension:

{x for x in foo if x not in bar}

Dictionary Comprehension:

{x: x for x in foo if x not in bar}

Generator Comprehension:

(x for x in foo if x not in bar)

When handled with some grace and care for readability, these can be extremely useful tools. But sometimes they can get, well, incomprehensible. With this in mind, I made incomprehensible: a collection of useful but totally unreadable python comprehensions.

Currently, there are 5 examples in the repo:

# dedupe list of dicts based on list of keys [v for _, v in {'_'.join([str(d.get(k, '')) for k in keys]): d for d in x_in}.items()] # flatten a dict with a nested list of dicts in it [dict([(k, v) for k, v in x_in.items() if k != key] + ch) for ch in [[(key + '.' + k, v) for k, v in y.items()] for y in x_in[key]]] # flatten a list of mixed types one level [y for z in [x if isinstance(x, list) else [x] for x in x_in] for y in z] # generator comprehension for a files with a certain extension in a directory (os.path.join(root, file) for root, _, files in os.walk(x_in) for file in files if file.endswith(extension)) # split a delimited string and coerce into basic types [int(y) if y.replace('-', '', 1).isdigit() else float(y) if y.replace('.', '', 1).replace('-', '', 1).isdigit() else y for y in [str(x).strip() for x in x_in.split(delimiter)]]

Have any more? Send me a pull request. Actually want to use these as a package?

$pip install git+https://github.com/wdm0006/incomprehensible.git >>import incomprehensible as inc >>inc.dedupe_dictlist(...)

Want to see the code?

https://github.com/wdm0006/incomprehensible