Python Chapter#9: Python Dictionary

Ankur Kulhari

  • Items of dictionaries are enclosed by curly braces ({})
  • Consist of key-value pairs
  • A dictionary key can be any Python data type,(usually numbers or strings)
  • Values, can be any Python object.
  • Values can be assigned and accessed using square braces ([])

Ex:

d1 = {}
d1[123] = "Hello"
d1["World"] = 123
d2 = {";name": "jonny","Employee_code":6103467, "dept": "CSE"}
print(d1[123])         # Prints value for 'one' key
print(d1["World"])     # Prints value for 2 key
print(d2)              # Prints complete dictionary
print(d2.keys())       # Prints all the keys
print(d2.values())     # Prints all the values

OUTPUT:

Hello
123
{'Employee_code': 6103467, 'dept': 'CSE', 'name': 'jonny'}
dict_keys(['Employee_code', 'dept', 'name'])
dict_values([6103467, 'CSE', 'jonny'])
Python Dictionary Functions
  • cmp(dict1, dict2): Compares elements of both dict.
  • len(dict): Gives the total length of the dictionary. This would be equal to the number of items in the dictionary.
  • str(dict): Produces a printable string representation of a dictionary
  • type(variable): Returns the type of the passed variable. If passed variable is dictionary, then it would return a dictionary type.
  • clear(): Removes all elements of dictionary. Ex: d1={‘Name’: ‘Jonny’, ‘Age’: 17}; d1.clear(); print(d1) prints nothing
  • copy(): Returns a shallow copy of dictionary. Ex: d2=d1.copy(), copies d1 to d2.
  • fromkeys(seq,Value): Create a new dictionary with keys from seq and values seq to value. Ex:
    seq = ('name', 'age', 'sex')
    
    dict = dict.fromkeys(seq)
    print "New Dictionary : %s" %  str(dict)
    
    dict = dict.fromkeys(seq, 10)
    print "New Dictionary : %s" %  str(dict)
    

    Output:

    New Dictionary : {'age': None, 'name': None, 'sex': None}
    New Dictionary : {'age': 10, 'name': 10, 'sex': 10}
    
  • get(key, default): For key key, returns value or default if key not in dictionary
  • has_key(key): Returns True if key in dictionary is found, False otherwise

  • items(): Returns a list of dictionary’s (key, value) tuple pairs
  • keys(): Returns list of dictionary dictionary’s keys
  • setdefault(key, default): Similar to get(), but will set d1[key]=default if key is not already in dict
  • update(d2): Adds dictionary d2‘s key-values pairs to dictionary. Ex: d1.update(d2), adds d2 to d1.
  • values(): Returns list of dictionary’s values
  • del dict[‘Name’]: remove entry with key ‘Name’ from dict.
  • dict.clear(): remove all entries in dict.
  • del dict: delete entire dictionary.
  • dict.has_key(key): returns true if key is there in dictionary dict, false otherwise.
  • dict.items(): returns a list of dict’s (key, value) tuple pairs.
  • dict.keys(): returns list of dictionary dict’s keys
  • dict1.update(dict2): adds key value pairs of dict2’s to dict1
  • dict.values(): returns all the values of dictionary dict.

Comments 1

  1. Pingback: Python Variable Types - Study Korner

What do you think about the article?