In real-world, dictionary is nothing but a source of finding meanings of different words present in any literature. In Python 3, the dictionary is termed as Key-Value pair where Key acts like a “Word” and Value acts as a meaning to the “Word”. In other words, we can assign any value to the key. And, remember elements in the Key are CaSe SensitIve.

Still confused? Don’t worry, it will be clear with an Example…

And, unlike in lists and tuples, the Key or say index not necessary to be an integer. It can be any string or any integer value as shown in below syntax of dictionary and it is always enclosed under curly brackets {}.

1Syntax:  dict = {'Start': 'Study', 2: 'at', 'end': 'LetsUpgrade'}

Here, dict is the name of built-in function and thus, it is better to avoid using as a variable.

1

2

3

>>> LetsUpgrade = dict()

>>> print(LetsUpgrade)

{}

The above {} represent an empty dictionary and to add items in a dictionary, we use square brackets [] as shown below:

1

2

3

>>> LetsUpgrade ['one'] = 'Batch'

>>> print(LetsUpgrade)

{'one': 'Batch'}

Thus, we successfully created a Dictionary and it forms a key-value pair with a colon between the key and value.

Now, let’s see the topics to be covered in Dictionary:

  • KeyError
  • Nested Dictionary
  • Adding elements to a Dictionary
  • Accessing elements from a Dictionary
  • Removing/Deleting Elements from Dictionary
  • Dictionary Methods
  • Membership operators
  • Python Iterate Dictionary
  • Dictionary as a set of counters
  • Dictionaries and files
  • Looping and dictionaries
  • Advanced text parsing
  • Python Dictionary Comprehension

#KeyError:

1dict = {'Start': 'Study', 2: 'at', 'end': 'LetsUpgrade'}

Suppose in above example, if the key isn’t in the dictionary, you get an exception as shown below:

1

2

>>> print(dict ['any value'])

KeyError: 'any value'

#Nested Dictionary:

Image source: GeeksForGeeks

1

2

3

4

5

# Creating a Nested Dictionary

# as shown in the below image

Dict = {1: 'Hi', 'Any': 'Upgraders', 3:{'A' : 'Welcome', 'B' : 'To', 'C' : 'LetsUpgrade Community'}}

print(Dict)

Output:

{1: 'Hi', 'Any': 'Upgraders', 3: {'A': 'Welcome', 'B': 'To', 'C': 'LetsUpgrade Community'}}

#Adding elements to a Dictionary:

1

2

3

4

5

6

# Adding and updating elements one at a time

Dict[0] = 'LetsUpgrade'

Dict[2] = 'Community'

Dict[3] = 01

print("\nDictionary after adding 3 elements: ")

print(Dict)

Output:

Dictionary after adding 3 elements: 

{1: 'Hi', 'Any': 'Upgraders', 3: 1, 0: 'LetsUpgrade', 2: 'Community'}

We just added and updated the new elements in the Nested Dictionary which we had generated.

#Accessing elements from a Dictionary:

1

2

3

4

5

6

7

8

9

10

11

# Accessing a element from a Dictionary

# Creating a Dictionary

Dict = {1: 'LetsUpgrade', 'Hi': 'New', 3: 'Batch'}

# accessing a element using key

print("Accessing a element using key:")

print(Dict['Hi'])

# accessing a element using key

print("Accessing a element using key:")

print(Dict[3])

Output:

Accessing a element using key: 

New 

Accessing a element using key:

 Batch

#Removing/Deleting Elements from Dictionary:

1

2

3

4

5

6

7

# Creating a Dictionary

Dict = {1: 'LetsUpgrade', 'Hi': 'New', 3: 'Batch'}

# Deleting or removing a Key value

del Dict[1]

print("\nDeleting a specific key: ")

print(Dict)

Output:

Deleting a specific key: 

{'Hi': 'New', 3: 'Batch'}

#Dictionary Methods:

Now, your work is to GOOGLE the below functions and understand how they work in Python Programming. (Exercise)

METHODSDESCRIPTION
copy()They copy() method returns a shallow copy of the dictionary.
clear()The clear() method removes all items from the dictionary.
pop()Removes and returns an element from a dictionary having the given key.
popitem()Removes the arbitrary key-value pair from the dictionary and returns it as tuple.
get()It is a conventional method to access a value for a key.
dictionary_name.values()returns a list of all the values available in a given dictionary.
str()Produces a printable string representation of a dictionary.
update()Adds dictionary dict2’s key-values pairs to dict
setdefault()Set dict[key]=default if key is not already in dict
keys()Returns list of dictionary dict’s keys
items()Returns a list of dict’s (key, value) tuple pairs
has_key()Returns true if key in dictionary dict, false otherwise
fromkeys()Create a new dictionary with keys from seq and values set to value.
type()Returns the type of the passed variable.
cmp()Compares elements of both dict.

(Part 2)

-Yogesh Raghupati (Author)