Python Dictionaries

Creating, Initialising and Accessing the Elements in a Dictionary

Dictionary in Python refers to an unordered collection of data values. Furthermore, its use takes place to store data values like a map, which holds only a single value as an element. Moreover, elements in a dictionary play a major role in Python programming.

elements in a dictionary

                                                                                                                     Elements in a Dictionary

Creating a Python Dictionary

In Python, a programming language, the creation of a dictionary can take place by placing sequence of elements within curly {} braces, whose separation takes place by ‘comma’. Furthermore, dictionary holds a pair of values, one is the pair element being its Key:value and the other being the Key.

The values in a dictionary can be of any of the data types and their duplication can also take place. In contrast, keys must be immutable and their repetition cannot place.

# Creating a Dictionary

# with Integer Keys

Furthermore, Dict = {1: ‘Toppr’, 2: ‘For’, 3: ‘Toppr’}

Moreover, print(“\nDictionary with the use of Integer Keys: “)

Also, print(Dict)

# Creating a Dictionary

# with Mixed keys

Furthermore, Dict = {‘Name’: ‘Toppr’, 1: [1, 2, 3, 4]}

Moreover, print(“\nDictionary with the use of Mixed Keys: “)

Also, print(Dict)

Output:

Dictionary with the use of Integer Keys:

{1: ‘Toppr’, 2: ‘For’, 3: ‘Toppr’}

 

Dictionary with the use of Mixed Keys:

{1: [1, 2, 3, 4], ‘Name’: ‘Toppr’}

The creation of the dictionary can also take place by the built-in function dict(). Furthermore, the creation of an empty dictionary can take place by just placing curly braces{}.

Furthermore, # Creating an empty Dictionary

Moreover, Dict = {}

print(“Empty Dictionary: “)

print(Dict)

 

Furthermore, # Creating a Dictionary

Also, # with dict() method

Dict = dict({1: ‘Toppr’, 2: ‘For’, 3:’Toppr’})

print(“\nDictionary with the use of dict(): “)

print(Dict)

 

Furthermore, # Creating a Dictionary

Moreover, # with each item as a Pair

Dict = dict([(1, ‘Toppr’), (2, ‘For’)])

print(“\nDictionary with each item as a pair: “)

print(Dict)

So, Output:

Empty Dictionary:

{}

 

Dictionary with the use of dict():

{1: ‘Toppr’, 2: ‘For’, 3: ‘Toppr’}

Dictionary with each item as a pair:

{1: ‘Toppr’, 2: ‘For’}

How to Initialize a Dictionary in Python?

There are various ways of initializing the elements in a dictionary. Furthermore, below is a description of initialization by using the fromkeys() method

# Python code to initialize a dictionary

# with only keys from a list

# List of keys

Student = [“Ram”, “Singh”, “Cyware”]

# using fromkeys() method

StudentDict = dict.fromkeys(Student, None)

# printing dictionary

print(StudentDict)

So, Output:

Finally, {‘Cyware’: None, ‘Singh’: None, ‘Ram’: None}

Accessing the Elements in a Dictionary

For accessing the elements in a dictionary, one must refer to its key name. Furthermore, the use of the key can take place inside square brackets.

# Python program to demonstrate

Moreover, # accessing an element from a Dictionary

Furthermore, # Creating a Dictionary

Dict = {1: ‘Toppr’, ‘name’: ‘For’, 3: ‘Toppr’}

Also, # accessing a element using key

print(“Accessing a element using key:”)

print(Dict[‘name’])

Moreover, # accessing a element using key

print(“Accessing a element using key:”)

print(Dict[1])

Furthermore, Output:

Accessing a element using key:

For

Furthermore, Accessing a element using key:

Toppr

There is also a method called get() that is available for accessing the elements in a dictionary.

# Creating a Dictionary

Dict = {1: ‘Geeks’, ‘name’: ‘For’, 3: ‘Geeks’}

 

# accessing a element using get()

# method

print(“Accessing a element using get:”)

print(Dict.get(3))

So, Output:

Accessing a element using get:

Finally, Toppr

FAQs For Creating, Initialising and Accessing the Elements in a Dictionary

Question 1: Briefly explain how to initialize an empty dictionary in Python?

Answer 1: Initialization of an empty dictionary is possible by making use of the in-built dict function. Furthermore, the initialization of empty dictionaries can also take place by using empty curly braces.

Question 2: Name the three ways of creating a dictionary in Python?

Answer 2: The three ways of creating a dictionary in Python are as follows:

  • Create Dictionary by Curly Brackets.
  • Create Dictionary by the Dict Function.
  • Lastly, create Dictionary by Dict Comprehension.
Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Browse

Python Dictionaries
  • Creating, Initialising and Accessing the Elements in a Dictionary

Leave a Reply

Your email address will not be published. Required fields are marked *

Browse

Python Dictionaries
  • Creating, Initialising and Accessing the Elements in a Dictionary

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.