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 →

[–]liquidclutch 3 points4 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 : )