Similar to sets, the dictionary data type in Python is a special data structure not commonly seen in other programming languages. In a dictionary, data is organized as key-value pairs. Values can be accessed using keys. Dictionaries are written inside curly braces.

# This program is an example of a dictionary in python
  
clothDict={"name":"Shirt",
    "size":"Long",
    "type":"Casual"
} # Defining a dict

print(clothDict)

Result-

{'name': 'Shirt', 'size': 'Long', 'type': 'Casual'}

Accessing a Specific Element

To access a specific value in a dictionary, you need the key.

# This program is an example of a dictionary in python
  
clothDict={"name":"Shirt",
    "size":"Long",
    "type":"Casual"
} # Defining a dict

outputDict=clothDict["name"]

print(outputDict) 

Result:

Shirt 

Similarly, to change a value, you use the key.

# This program is an example of a dictionary in python
  
clothDict={"name":"Shirt",
    "size":"Long",
    "type":"Casual"
} # Defining a dict

clothDict["size"]="small"

print(clothDict) 

Result:

{'type': 'Casual', 'size': 'small', 'name': 'Shirt'}  

Looping Through a Dictionary

Using a loop, you can get all the keys in a dictionary.

# This program is an example of loop in a dictionary in python
  
clothDict={"name":"Shirt",
    "size":"Long",
    "type":"Casual"
} # Defining a dict

for i in clothDict: 
    print(i)

Result:

name                                                                                                                                                                               
type                                                                                                                                                                               
size

Since the key is obtained through the loop, the value can also be extracted in the same way as before.

# This program is an example of a loop in a dictionary in python
  
clothDict={"name":"Shirt",
    "size":"Long",
    "type":"Casual"
} # Defining a dict

for i in clothDict: 
    print(clothDict[i])

Result:

Shirt                                                                                                                                                                              
Casual                                                                                                                                                                             
Long

Dictionary Methods

Dictionaries have several built-in methods. Let’s look at some of them (in alphabetical order).

clear()

The clear() method removes all items from the dictionary.

# This program is an example of dictionary clear() method in python
  
clothDict={"name":"Shirt",
    "size":"Long",
    "type":"Casual"
} # Defining a dict

clothDict.clear() # Clear() classmethod

print(clothDict)

Result:

{}

copy()

The copy() method copies the dictionary to another dictionary.

# This program is an example of dictionary copy() method in python
  
clothDict={"name":"Shirt",
    "size":"Long",
    "type":"Casual"
} # Defining a dict

outputDict=clothDict.copy() # copy() classmethod

print(outputDict)

Result:

{'name': 'Shirt', 'type': 'Casual', 'size': 'Long'}

del()

The del statement deletes a specific element from the dictionary.

# This program is an example of dictionary del() method in python
  
clothDict={"name":"Shirt",
    "size":"Long",
    "type":"Casual"
} # Defining a dict

del clothDict["size"] # del() method

print(clothDict)

Result:

{'name': 'Shirt', 'type': 'Casual'}

You can also use del to delete the entire dictionary.

# This program is an example of dictionary del() method in python
  
clothDict={"name":"Shirt",
    "size":"Long",
    "type":"Casual"
} # Defining a dict

del clothDict # del() method

print(clothDict)

Result:

Traceback (most recent call last):                                                                                                                                                 
  File "main.py", line 10, in <module>                                                                                                                                             
    print(clothDict)                                                                                                                                                               
NameError: name 'clothDict' is not defined

get()

The get() method accesses a specific element in the dictionary.

# This program is an example of dictionary get() method in python
  
clothDict={"name":"Shirt",
    "size":"Long",
    "type":"Casual"
} # Defining a dict

outputValue=clothDict.get("name") # get() classmethod

print(outputValue)

Result-

Shirt

keys()

The keys() method returns all the keys in the dictionary.

# This program is an example of dictionary keys() method in python
  
clothDict={"name":"Shirt",
    "size":"Long",
    "type":"Casual"
} # Defining a dict

outputValue=clothDict.keys() # keys() classmethod

print(outputValue)

Result:

dict_keys(['type', 'size', 'name'])

len()

The len() method returns the number of items in the dictionary.

# This program is an example of dictionary len() method in python
  
clothDict={"name":"Shirt",
    "size":"Long",
    "type":"Casual"
} # Defining a dict

dictLength=len(clothDict) # len() method

print(dictLength)

Result:

3

pop()

The pop() method removes a specific item from the dictionary and returns its value.

# This program is an example of the dictionary pop() method in python
  
clothDict={"name":"Shirt",
    "size":"Long",
    "type":"Casual"
} # Defining a dict

outputValue=clothDict.pop("type") # pop() method

print(outputValue) # popped value
print(clothDict)

Result:

Casual                                                                                                                                                                             
{'name': 'Shirt', 'size': 'Long'} 

popitem()

The popitem() method removes a random item from the dictionary and returns it.

# This program is an example of dictionary popitem() method in python
  
clothDict={"name":"Shirt",
    "size":"Long",
    "type":"Casual"
} # Defining a dict

outputValue=clothDict.popitem() # popitem() method

print(outputValue) # popped value
print(clothDict)

Result:

('size', 'Long')                                                                                                                                                                   
{'type': 'Casual', 'name': 'Shirt'}

setdefault()

The setdefault() method sets a default value if the specified key does not exist.

# This program is an example of dictionary setdefault() method in python
  
clothDict={"name":"Shirt",
    "size":"Long",
    "type":"Casual"
} # Defining a dict

outputValue=clothDict.setdefault("size","short") # setdefault() method
print(outputValue)
print(clothDict)

outputValue2=clothDict.setdefault("color","white") # setdefault() method
print(outputValue2)
print(clothDict)

Result:

Long                                                                                                                                                                               
{'size': 'Long', 'type': 'Casual', 'name': 'Shirt'}                                                                                                                                
white                                                                                                                                                                              
{'size': 'Long', 'color': 'white', 'type': 'Casual', 'name': 'Shirt'}

update()

The update() method updates the dictionary with the specified key-value pairs.

# This program is an example of dictionary update() method in python
  
clothDict={"name":"Shirt",
    "size":"Long",
    "type":"Casual"
} # Defining a dict

clothDict.update({"color":"white"}) # update() method

print(clothDict)

Result:

{'type': 'Casual', 'name': 'Shirt', 'color': 'white', 'size': 'Long'}

values()

The values() method returns all the values in the dictionary.

# This program is an example of dictionary values() method in python
  
clothDict={"name":"Shirt",
    "size":"Long",
    "type":"Casual"
} # Defining a dict

outputDict=clothDict.values() # values() method

print(outputDict)

Result:

dict_values(['Long', 'Shirt', 'Casual'])
0 0 votes
Article Rating
0
Would love your thoughts, please comment.x
()
x