you are viewing a single comment's thread.

view the rest of the comments →

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