Change Values

You can change the value of a specific item by referring to its key name:

Example

Change the "year" to 2018:

thisdict = {

  "brand": "Ford",

  "model": "Mustang",

  "year": 1964

}

thisdict["year"] = 2018

Loop Through a Dictionary

You can loop through a dictionary by using a for loop.

When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.

Example

Print all key names in the dictionary, one by one:

for x in thisdict:

 print(x)

Example

Print all values in the dictionary, one by one:

for x in thisdict:

 print(thisdict[x])

Example

You can also use the values() function to return values of a dictionary:

for x in thisdict.values():

 print(x)

Example

Loop through both keys and values, by using the items() function:

for x, y in thisdict.items():

 print(x, y)

Jay Kakadiya

Jay Kakadiya Creator

I am a computer field, & i am Web developer.

Suggested Creators

Jay Kakadiya