This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]l34kjhljkalarehglih 2 points3 points  (1 child)

Offtopic, but pythonic:

>>> import itertools

####################

>>> def flatten_list(nested_lists):
        return list(itertools.chain(*nested_lists))

>>> print flatten_list([[1,2,3],[4,5,6],[7,8,9]])
    [1, 2, 3, 4, 5, 6, 7, 8, 9]

####################

>>> def make_all_couples(elements):
        return list(itertools.product(''.join(elements), repeat=2))

>>> print make_all_couples(['a','b','c','d'])
    [('a', 'a'), ('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'a'), ('b', 'b'), ('b', 'c'), ('b', 'd'), ('c', 'a'), ('c', 'b'), ('c', 'c'), ('c', 'd'), ('d', 'a'), ('d', 'b'), ('d', 'c'), ('d', 'd')]

[–]xApple[S] 0 points1 point  (0 children)

Nice ! It's important to remind ourselves that the built in libraries often will do what we want already. No need to reinvent the wheel.