Categories: python

Python Dictionary

Python Dictionary is used to store the data in a key-value pair format. Python’s dictionary data type can mimic real-world data arrangements where a particular value exists for a particular key. The mutable data structure is what it is. The element Keys and values is used to define the dictionary.

  • Keys must consist of just one thing.
  • Any type of value is acceptable, including list, tuple, integer, etc.

Example:

Dict = {1: 'Rahul', 2: 'Shruti', 3: 'Ron'}
print(Dict)

Output:

{1: 'Rahul', 2: 'Shruti', 3: 'Ron'}
Dictionary and List:

The following traits apply to both dictionaries and lists:

  • Both are mutable.
  • Both are dynamic. They can expand and contract as necessary.
  • Both can be nested. One list may include another list. Another dictionary may be found inside one. A list can also be found in a dictionary, and vice versa.

Lists and dictionaries differ primarily in the way that elements are accessed:

  • Indexing allows access to list elements based on their location within the list.
  • Keys are used to accessing dictionary elements.
Dictionary with integer keys:
Dict = {1: 'Rahul', 2: 'Shruti', 3: 'Ron'}
print(Dict)
Dictionary with String keys:
Dict = {'ab': 'Rahul', 'cd': 'Shruti', 'ef': 'Ron'}
print(Dict)
Empty Dictionary:
Dict = {}
print("Empty Dictionary: ")
print(Dict)

Output:

Empty Dictionary: 
{}
Nested Dictionary:

Dict = {1: 'Maths', 2: 'HIndi',
  3: {'S1': 'Biology', 'S2': 'Chemistry', 'S3': 'physics'}}

print(Dict)

Output:

{1: 'Maths', 2: 'HIndi', 3: {'S1': 'Biology', 'S2': 'Chemistry', 'S3': 'physics'}}
Accessing elements of a dictionary:
Dict = {'ab': 'Rahul', 'cd': 'Shruti', 'ef': 'Ron'}
print(Dict)
for i in Dict:
    print ("Key: " + i + " and Element: " + Dict[i]);

Output:

{'ab': 'Rahul', 'cd': 'Shruti', 'ef': 'Ron'}
Key: ab and Element: Rahul
Key: cd and Element: Shruti
Key: ef and Element: Ron
Deleting element(s) in a dictionary:

Deleting elements with the del keyword is similar to removing items from a list.

Example:


Dict = {1: 'Maths', 2: 'HIndi',
  3: {'S1': 'Biology', 'S2': 'Chemistry', 'S3': 'physics'}}
del Dict[3]
print(Dict)

Output:

{1: 'Maths', 2: 'HIndi'}

Note: also read about Remove elements from a Python List

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago