What are dictionaries in python and how to work with them ?

Dictionaries is a special type of content holder used in python just like an array.

In array, we reference a value using an index number, however in a dictionary we create a key-value pair, and refer to keys for setting/changing values.

Creating Dictionary empdb containing employee name and its ID.

$ cat dictionary 
#!/usr/bin/python

### Creating a dictionary empdb
empdb = {
	"Jack" : 1239,
	"Jones" : 5420
}

### Printing dictionary
print(empdb)

### Adding item into it
empdb["Mohan"] = 8888

### Deleting item
empdb.pop("Jack")

### print the final dictionary data
print(empdb)
$ 

 

Output

$ ./dictionary 
{'Jones': 5420, 'Jack': 1239}
{'Jones': 5420, 'Mohan': 8888}
$
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments