Sitemap

Python — Dictionary

2 min readApr 22, 2018

--

Dictionary is an unordered set of key and value pair. It is a container that contains data, enclosed within curly braces. the pair i.e., key and value is known as item. They key passed in the item must be unique.

The key and the value is separated by a colon(:). This pair is known as item. Items are separated from each other by a comma(,). Different items are enclosed within a curly braces and this forms Dictionary.

dic = {1:"A", 2:"B", 3:"C", 4:"D"}
print(dic) #Output : {1: 'A', 2: 'B', 3: 'C', 4: 'D'}

Dictionary is mutable i.e., value can be updated.

Get Anandesh Sharma’s stories in your inbox

Join Medium for free to get updates from this writer.

Key must be unique and immutable. Value is accessed by key. Value can be updated while key cannot be changed. Dictionary is known as Associative array since the Key works as index and they are decided by the user.

Example:Dic = {}

#Values in brackets are keys and Values on right side are data values
Dic[1] = "A"
Dic[2] = "B"
Dic[3] = "C"
Dic["a"] = "Louis"
Dic["b"] = "Williams"

print(Dic) #Output : {1: 'A', 2: 'B', 3: 'C', 'a': 'Louis', 'b': 'Williams'}

ACCESSING DICTIONARY VALUES

Since Index is not defined, a Dictionary values can be accessed by their keys only. It means, to access the dictionary elements we need to pass key, associated to the value.

Std = {1:"Andy", 2:"Bash", 3:"Cayle"}

print("First Student : ", Std[1])
print("Second Student : ",Std[2])
print("Third Student : ", Std[3])

# Output :
# First Student : True
# Second Student : Bash
# Third Student : Cayle

UPDATING PYTHON DICTIONARY

The item i.e., key-value pair can be updated. Updating means new item can be added to the dictionary. The values can be modified.

Std = {1:"Andy", 2:"Bash", 3:"Cayle"}

Std[2] = "Bob"

print(Std) #Output : {1: 'Andy', 2: 'Bob', 3: 'Cayle'}

DELETING ELEMENTS FROM PYTHON DICTIONARY

del statement can be used for performing deletion operation in dictionary. An element can be deleted using the key only.

Std = {1:"Andy", 2:"Bash", 3:"Cayle"}

del(Std[2])

print(Std) #Output : {1: 'Andy', 3: 'Cayle'}

PYTHON DICTIONARY FUNCTIONS

cmp(dict1, dict2) : No longer available in Python 3.

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.

Std = {1:"Andy", 2:"Bash", 3:"Cayle"}
#Returns all keys
print(Std.keys())
#Returns all values
print(Std.values())
#Returns all the items(key-value) pair
print(Std.items())
#Returns ordered copy of the data
print(Std.copy())
#Returns the data using key
print(Std.get(1))
#Used to add items of another dictonary
print(Std.update())

#Output :
# dict_keys([1, 2, 3])
# dict_values(['Andy', 'Bash', 'Cayle'])
# dict_items([(1, 'Andy'), (2, 'Bash'), (3, 'Cayle')])
# {1: 'Andy', 2: 'Bash', 3: 'Cayle'}
# Andy
# None

--

--

Anandesh Sharma
Anandesh Sharma

Written by Anandesh Sharma

A software engineer specializing in building exceptional digital experiences. Currently, I’m focused on building accessible, human-centered products