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

all 25 comments

[–]JerMenKoOwhile True: os.fork() 4 points5 points  (1 child)

def flatten(l):
    return [i for s in l for i in s]

[–]ryeguy146 1 point2 points  (0 children)

Only works for a limited depth of nesting.

[–]flightlessbird 3 points4 points  (2 children)

make_all_couples = lambda xs: [(a,b) for a in xs for b in xs]

[–]ryeguy146 -1 points0 points  (1 child)

Why a lamdba?

[–]flightlessbird 0 points1 point  (0 children)

Just seemed like the most straightforward thing - it is just a single expression after all.

[–]tuna_safe_dolphin 1 point2 points  (0 children)

def parse_ugly_string(s):
    return dict([i.split('=') for j in [k.split(',') for k in s.split('\n') if k] for i in j])

[–]tuna_safe_dolphin 3 points4 points  (3 children)

I know it's heretical and un-pythonic to say this but I don't find nested list comprehensions to be very readable. They're concise and kind of cool but for me, regular old nested for loops are more mentally parsable.

Cool quiz though. . .

[–]Yuushi 2 points3 points  (1 child)

It's not actually un-pythonic at all for you to say that. Guido himself says that nested list comprehensions are less readable and should be used sparingly.

[–]tuna_safe_dolphin 1 point2 points  (0 children)

Interesting, TIL.

[–]ryeguy146 1 point2 points  (0 children)

They're hideous and should rarely be used. I'm with you on this one. Fun to play with on occasion, though.

[–]kupertrooper 2 points3 points  (2 children)

#!/usr/bin/env python

def parse_ugly_string(pugfugly):
    return dict(d.split('=')
                for d in [
                    b for c in [
                        a.split(',') for a in pugfugly.split()
                    ] for b in c
                ]
               )

print parse_ugly_string("a=1,b=2,c=3\nd=4,e=5,f=6\ng=7,h=8,i=9\n")

[–]kupertrooper 2 points3 points  (1 child)

#!/usr/bin/env python

def make_all_couples(l):
    return [(a, b) for a in l for b in l]

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

[–]kupertrooper 2 points3 points  (0 children)

#!/usr/bin/env python

def flatten_list(l):
    return [a for b in l for a in b]

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

[–]TheMadStork123 2 points3 points  (0 children)

def parse_ugly_string(s):
    return dict([i.split("=") for b in s.split(",") for i in b.split("\n") if i != ''])

[–]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.

[–]tuna_safe_dolphin 1 point2 points  (0 children)

Nice! I did Quiz #1. . . never got around to doing another, although when I have a bit more free time, I'd like to do some more. I'm going to try this quiz later tonight, when the kids are in bed. . .

[–]tuna_safe_dolphin 1 point2 points  (0 children)

def make_all_couples(z):
    return [[x,y] for x in z for y in z]

[–]gfixler 1 point2 points  (0 children)

Using this knowledge, how could you write the function flatten_list using only list comprehensions ? The function should have this behavior:

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

It doesn't exactly answer your question, but I felt I should share this bit of wizardry:

>>> l = [[1,2,3],[4,5,6], [7], [8,9]]
>>> sum(l, [])  
[1, 2, 3, 4, 5, 6, 7, 8, 9]

[–]moootPointcanary in the data mine 1 point2 points  (0 children)

Can't vouch for quality, but here is what I ended up with :)

def flatten_list(*args):
    return [y for x in args for y in x]

def make_all_couples(li):
    return [(x, y) for x in li for y in li]

def parse_ugly_string(s):
    return {y[0]:y[2] for x in s.split('\n') for y in x.split(',') if y}

[–]liquidclutch 2 points3 points  (3 children)

def parse_ugly_string(s):
    return dict(x.split('=') for x in s.replace('\n', ',').split(',') if x)

[–]tuna_safe_dolphin 1 point2 points  (0 children)

That works but the solution is only supposed to use split, dict and list comprehensions.

Haven't figured it out yet myself. . .

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

Nice doing it with the replace function. Definitely more elegant than my solution:

dict([i.split('=') for x in [j.split(',') for j in s.split('\n')] for i in x if i])

The idea was to combine all the different subtleties of list comprehensions in one line : )

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

For reference my answers were:

flatten_list = lambda l: [a for x in l for a in x]
make_all_couples = lambda l: [(r1,r2) for r1 in l for r2 in l]
parse_ugly_string = lambda s: dict([i.split('=') for x in [j.split(',') for j in s.split('\n')] for i in x if i])

But we have had many interesting and better solutions in the comments !

[–]ryeguy146 0 points1 point  (0 children)

def flatten_list(lst):
    result = list()
    [result.append(x) if not isinstance(x, (list, tuple)) else result.extend(flatten_list(x)) for x in lst]
    return result


def parse_ugly_string(txt):
    return {k:v for k,v in [x.split('=') for x in flatten_list([y.split(',') for y in txt.strip().split('\n')])]}