Python Dictionary: Update, Len, Sort, Copy With Examples

Python dictionary is another collection object in Python. Here the elements are changeable, indexed, and unordered. Dictionary is identified with curly brackets and each item is shown as a key-value pair or format. Each key is divided by a colon (:) and the elements are detached by commas (,) in the dictionary. It is also possible to create a blank dictionary like {}. The keys should be an immutable data type in Python dictionary, but the values can be any type. We have to keep in mind that when the key is known, it will be easy to retrieve values from the dictionary. Values may not be unique, but keys must be unique. The syntax or structure is given below,

mydict = {
  "Name": "John",
  "Age": 30,
  "Address": "USA"
}
print(mydict) 

# Output
#{'Age': 30, 'Name': 'John', 'Address': 'USA'}

Dictionary Item Access

By using the key name, we can access the items of the dictionary. We have to put the key name into the square bracket. By using the get() method, we can identify the item of the key. Examples are given below,

mydict = {
  "Name": "John",
  "Age": 30,
  "Address": "USA"
}
a = mydict["Address"]
print(a) 
b = mydict.get("Address")
print(b)

# Output
#USA
#USA

Change Values in Dictionary

We can change a specific item’s value in a dictionary. For such an operation, we have to refer the key name and assign a new value to it. An example is given below,

mydict = {
  "Name": "John",
  "Age": 30,
  "Address": "USA"
}
mydict["Address"]="UK"
print(mydict) 

# Output
# {'Age': 30, 'Name': 'John', 'Address': 'UK'}

For Loop in Dictionary

We can print all the keys and values through for loop. If we print the iteration, then it will return the keys only. But if we print the iteration as an index of the dictionary then it will return the values only. We also can use the values method for returning the values. We can return both keys and values by using the items() method. Examples are given below,

mydict = {
  "Name": "John",
  "Age": 30,
  "Address": "USA"
}

for i in mydict:
    print(i)

# Output
# Age Name Address

for j in mydict:   
    print(mydict[j])

# Output
#30 John USA

for k in mydict.values():
    print(k) 

# Output
# 30 John USA  

for a, b in mydict.items():
    print(a, b) 

# Output
# ('Age', 30) ('Name', 'John') ('Address', 'USA')

Key Existence

By using the membership keyword ‘in’, we can check if a particular key is present or not. An example is given below,

mydict = {
  "Name": "John",
  "Age": 30,
  "Address": "USA"
}

if "Age" in mydict:
   print("Yes")

Length of Dictionary

We can measure the length of a dictionary by using the len() keyword. It will calculate how many items or key-value pairs are present in the dictionary. An example is given below,

mydict = {
  "Brand": "Apple",
  "Year": 1980
}
print(len(mydict))

# Output
# 2

Add Items in Dictionary

By using the new index key, we can add a new item (key-value) into a dictionary. An example is given below,

mydict = {
  "Brand": "Apple",
  "Year": 1980
}
mydict["Color"]= "Blue"
print(mydict) 

# Output
# {'Color': 'Blue', 'Brand': 'Apple', 'Year': 1980}

Remove Item from Dictionary

pop(), popitem(), del and clear() methods are used to remove items from a dictionary.

  • The pop() method is used for a specific key name.
  • The popitem() method always removes the last item inserted in the dictionary.
  • The del keyword is also used to remove an item with a particular key name. It is also used to delete the dictionary completely.
  • The clear() method is used to clear or remove all the items from the dictionary.

Examples are given below

 mydict = {
  "Brand": "Apple",
  "Year": 1980,
  "Color": "Blue",
  "Cost": 5000
}
print(mydict) 

# Output
#{'Color': 'Blue', 'Brand': 'Apple', 'Cost': 5000, 'Year': 1980}

mydict.pop("Cost")
print(mydict) 

# Output
#{'Color': 'Blue', 'Brand': 'Apple', 'Year': 1980}

mydict.popitem()
print(mydict) 

# Output
#{'Brand': 'Apple', 'Year': 1980}

del mydict["Brand"] # del mydict is used to the entire dictionary

mydict.clear()
print(mydict) 

# Output
#{}