from operator import concat reduce(concat, list_of_lists) # : sum(list_of_lists, []) # : from itertools import chain list(chain.from_iterable(list_of_lists)) from funcy import cat cat(list_of_lists) cat() combines a list of lists, tuples, iterators, and indeed any iterated into one list. If you want to combine lists of the results of a function call, you can use mapcat() , for example: from funcy import mapcat mapcat(str.splitlines, bunch_of_texts) icat() and imapcat() . d1.update(d2) # d1 dict(d1, **d2) # > 2 d = d1.copy() d.update(d2) from funcy import merge, join merge(d1, d2) merge(d1, d2, d3) join(sequence_of_dicts) merge() and join() can combine not only dictionaries, they work for almost any collections: dictionaries, ordered dictionaries, sets, lists, tuples, iterators, and even strings. m = re.search(some_re, s) if m: actual_match = m.group() # m.group(i), m.groups() ... from funcy import re_find actual_match = re_find(some_re, s) from funcy import re_finder, re_all, partial, mapcat # map(re_finder('\d+'), words) # ini (re_finder() > 1 ) dict(imap(re_finder('(\w+)=(\w+)'), ini.splitlines())) # ( ) mapcat(partial(re_all, r'\d+'), bunch_of_strings) from funcy import * re_finder() and partial() . It should be added that the re_finder() function itself is a partial use of re_find() created for ease of use in map() and the like. And naturally, with filter() it is convenient to use re_tester() : # is_private = re_tester('^_') filter(is_private, dir(some_obj)) is_private() , and filter object attributes by them: is_special = re_tester('^__.+__$') is_const = re_tester('^[A-Z_]+$') filter(...) is_public = complement(is_private) is_private_const = all_fn(is_private, is_const) either_const_or_public = any_fn(is_const, is_public) filter() : remove(is_private, ...) # , filter(is_public) walk() and select() functions, which are similar to map() and filter() , but retain the type of collection being processed: walk(inc, {1, 2, 3}) # -> {2, 3, 4} walk(inc, (1, 2, 3)) # -> (2, 3, 4) # - swap = lambda (k, v): (v, k) walk(swap, {1: 10, 2: 20}) # -> {10: 1, 20: 2} select(even, {1, 2, 3, 10, 20}) # -> {2, 10, 20} select(lambda (k, v): k == v, {1: 1, 2: 3}) # -> {1: 1} walk_keys(), walk_values(), select_keys(), select_values() : # select_keys(is_public, instance.__dict__) # select_values(bool, some_dict) silent() - silences all exceptions thrown by the wrapped function, returning None ; compact() - removes None values from the collection; walk_values() - bypasses the values of the passed dictionary by constructing a new dictionary with the values converted by the passed function. In general, this line selects a dictionary of integer parameters from the query parameters: compact(walk_values(silent(int), request_dict)) # URL absolute, relative = split(re_tester(r'^http://'), urls) # group_by(lambda post: post.category, posts) # dict(partition(2, flat_list_of_pairs)) # {id: (name, password) for id, name, password in partition(3, users)} # , assert all(prev + 1 == next for prev, next in partition(2, 1, versions)): # for chunk in chunks(CHUNK_SIZE, lots_of_data): process(chunk) # for line, prev in with_prev(text.splitlines()): if not prev: print ' ', print line # 1611 where(plays, author="Shakespeare", year=1611) # => [{"title": "Cymbeline", "author": "Shakespeare", "year": 1611}, # {"title": "The Tempest", "author": "Shakespeare", "year": 1611}] Source: https://habr.com/ru/post/174619/
All Articles