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.
Example:
Dict = {1: 'Rahul', 2: 'Shruti', 3: 'Ron'}
print(Dict)
Output:
{1: 'Rahul', 2: 'Shruti', 3: 'Ron'}
The following traits apply to both dictionaries and lists:
Lists and dictionaries differ primarily in the way that elements are accessed:
Dict = {1: 'Rahul', 2: 'Shruti', 3: 'Ron'}
print(Dict)
Dict = {'ab': 'Rahul', 'cd': 'Shruti', 'ef': 'Ron'}
print(Dict)
Dict = {}
print("Empty Dictionary: ")
print(Dict)
Output:
Empty 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'}}
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 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
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…