Why Would You Use the enumerate Keyword with a dictionary in Python?

Problem scenario
You see a dictionary used with the enumerate reserved word in Python.

Here is an example:

good_dict = {}
good_dict["size"] = "medium"
good_dict["quantity"] = "80"
good_dict["location"] = "New_York"
good_dict["phone"] = "555-555-5555"

for idx1, good_key in enumerate(good_dict):

A counter (a unique integer) can be assigned to each key. For logic-building purposes, it is beneficial. The above snippet would prepare a for loop for logic with 1) integers uniquely being assigned to the keys of the dictionary and 2) the keys of the dictionary. For arithmetic or other processing, the enumerate function can provide a quick benefit.

Having the key being accessible like that lends itself to sending to the dictionary like this: good_dict[good_key]

You will be able to iterate through with a unique integer too.

Leave a comment

Your email address will not be published. Required fields are marked *