Python Dictionary :

Creating and Accessing Values in Dictionary :

A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly ( { } ) brackets, and they have keys and values. Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples. To access dictionary elements, you can use the familiar square ( [ ] ) brackets along with the key to obtain its value.

Example :

  1. ourdict={
  2. "Name" : "AyaN",
  3. "Age" : 24,
  4. "Website" : "Techbaz"} #Create a dictionary (use '{ }' curly brackets )
  5. print(ourdict) #print full dictionary keys
  6. x=ourdict["Website"] #Access the items of a dictionary (use '[ ]'square brackets )
  7. print("\n Website Name : " + x) #Get the value of the "website" key
  8. input()

Output :

python dictionary creation and accessing

Change Values (with methods) :

You can change the value of a specific item by referring to its key name or you can use methods to manipulate dictionary key values.

Example :

  1. ourdict={
  2. "Name" : "AyaN",
  3. "Age" : 24,
  4. "Website" : "Techbaz"}
  5. print(ourdict) #print real key values before manipulating
  6. x = ourdict.get("Name") # get() method also get the value of the "Name" key
  7. print("Get only name key value : "+x)
  8. ourdict["Website"] ="AyaN Software" #Change the website "Techbaz" to "AyaN Software"
  9. print(ourdict) #print updated key values
  10. ourdict["Year"] = 2019 #Adding an item to the dictionary
  11. print(ourdict) #print updated key values
  12. ourdict.pop("Age") #The pop() method removes the item with the specified key name
  13. print(ourdict ) #print without age key value
  14. input()

Output :

python dictionary key values manipulate with methods

Dictionaries methods :

Python includes many built-in methods but here we just adding some of the following built-in methods to manipulate dictionaries :

  • The get() method to returns the value of the specified key.
  • The pop() method to removes the element with the specified key
  • The len() method to determine how many items (key-value pairs) a dictionary has.
  • The values() method to Returns a list of all the values in the dictionary.
  • The keys() method returns a list containing the dictionary's keys.
  • The copy() method to returns a copy of the dictionary.
  • The clear() method to removes all the elements from the dictionary.
  • The items() method to returns a list containing the a tuple for each key value pair.

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 :

  1. ourdict={
  2. "Name" : "AyaN",
  3. "Age" : 24,
  4. "Website" : "Techbaz"}
  5. for x in ourdict: print(x) #Print all key names in the dictionary, one by one
  6. print("\n - New Line 0 -\n")
  7. for x in ourdict: print(ourdict[x]) #Print all values in the dictionary, one by one
  8. print("\n - New Line 1 -\n")
  9. for x in ourdict.values(): print(x) #You can also use the values() method
  10. print("\n - New Line 2 -\n")
  11. for x, y in ourdict.items():
  12. print(x +" : ", y) #Loop through both keys and values, by using the items() method
  13. input()

Output :

python - loop(for) through a dictionary

Check if Key Exists :

To determine if a specified key is present in a dictionary use the in keyword.

Example :

  1. ourdict={
  2. "Name" : "AyaN",
  3. "Age" : 24,
  4. "Website" : "Techbaz"}
  5. if "Name" in ourdict: #Check if "Name" is present in the dictionary
  6. print("Yes, 'Name' is one of the keys in the ourdict dictionary")
  7. input()

Output :

python - dictionary with if statement

Computer Science Engineering

Special Notes

It's a special area where you can find special questions and answers for CSE students or IT professionals. Also, In this section, we try to explain a topic in a very deep way.

CSE Notes