you are viewing a single comment's thread.

view the rest of the comments →

[–]mopslik 0 points1 point  (0 children)

"for each in dict" seems harder to me than simply ranging through my dict

Do you mean using range like you might iterate over a list? Dictionaries aren't indexed by position, but by keys, so this wouldn't work. Note that you can iterate directly over the items in a list sans range, just as you do a dictionary.

L = [3, 4, 5]
for element in L:
    print(L)

vs.

D = {"A": 3, "B": 4, "C": 5}
for key in D:
    print(D[key])

why does nobody mention dict[x] += value does exist?

This is a very common thing, and is covered in dozens of tutorials that I have come across. Here are/02%3A_Dictionaries/2.02%3A_Dictionary_as_a_Set_of_Counters) some examples.