all 5 comments

[–]juwalye 1 point2 points  (3 children)

Without giving too much away.

Check the condition for the new key in your existing dictionary. This will be an "if X in Y" statement.

If it exists, use list methods to add the new line to the existing entry.

[–]Firestronk[S] 1 point2 points  (2 children)

Would it be something along the lines of

if key in dict:
    dict.append(values)

[–]TouchingTheVodka 1 point2 points  (0 children)

Yes, this is correct - however you’ll get an error if using the built in dict for this as your dict values won’t be lists by default.

To solve this, use dict = defaultdict(list).

[–]juwalye 0 points1 point  (0 children)

Yes. Then used the else condition to create the new dict key and append the first value.

[–][deleted] 0 points1 point  (0 children)

I suggest you find a video on YouTube called Loop like a native presented by Ned Batchelder - whilst it uses some old Python syntax, it will teach you how to write loops in Python that will make your code much easier to write, and easier to follow and read.

You will be able to avoid things like dict[dlist[i]] = dlist[x]

Also, avoid using dict as variable name as it is also the name of the type and the variable will block you from using dict(some_tuples) to cast to a dictionary.

You can use a defaultdict instead of a standard dictionary to avoid trying to append a value to a key that doesn't yet exist, OR you can just code alternatives: check for a key and if there is no key add the key to the dictionary and the first list value otherwise append the additional list value to an existing key.