Homepage
Sign in / Sign up
Go to the profile of Farid Smai
Farid Smai
Sep 16, 2016
12 versions of the same algorithm in JavaScript, Python, and Go
kentquirk
5011

In python, you can keep a regular dict and use dict.setdefault() instead of dict.get() to reduce the number of line in the loop:

output = dict()

for elem in input: output.setdefault(elem[‘owner’], []).append(elem[‘pet’])

  • Go to the profile of Farid Smai

    Farid Smai

    • Share
    Go to the profile of Farid Smai
    Never miss a story from Farid Smai, when you sign up for Medium. Learn more
    Never miss a story from Farid Smai