4 Ways of Creating Python Dictionary

Python Dictionary is a collection of key: value pairs, which is used to store data values.

You can think of a Dictionary in Python as a real-world dictionary with Keys – the words you look up in the dictionary and Values – the definitions of the words.

Python Dictionary

Example of Dictionary in Python

Here’s a simple example of a Python dictionary:

my_dict = {
    "Name""sssgeeks.com",
    "Type""Website",
    "year": 2023
}
Output:
{'Name': 'sssgeeks.com', 'Type': 'Website', 'year': 2023}

Creating a Dictionary in Python

There are several ways of creating a dictionary in Python. Here are the most common ways:

  • Using Curly Braces: You can create a Python dictionary by using curly braces {} and key-value.
    my_dict = {
    "key_1": "value1",
    "key_2": "value2",
    "key_3": "value3"}
  • Using dict() Constructor: You can create a Python dictionary by using the dict() constructor and passing key-value pairs as an argument.
my_dict = dict([ ("key1", "value1"), ("key2", "value2"), ("key3", "value3"))
  • Using a Comprehension: You can create a Python dictionary using dictionary comprehension, which allows you to create a dictionary based on an iterable.
my_dict = {key: value for key, value in zip(keys, values)}
  • Using the zip() Function: You can create a Python dictionary by zipping two lists together.
keys = ["key1", "key2", "key3"]
values = ["value1", "value2", "value3"]
my_dict = dict(zip(keys, values)) 

Nested Dictionary in Python

# Creating a Nested Dictionary in python
Dict = {1: 'sscgeeks', 2: 'For',3: {'A': 'Welcome', 'B': 'To', 'C': 'sscgeeks'}}

print(Dict)

Adding elements to a Python Dictionary

There are various ways of adding an element. One value can be added to a Dictionary by defining value with the key (Dict[Key] = ‘Value’).

We can update an existing value in a Dictionary by using the built-in Update() method. Updating an existing value in a Dictionary can be done by using the built-in update() method.

we can also add nested key values to an existing Dictionary.

# Creating an empty Python Dictionary
my_dict= {}
print(my_dict)
# Adding elements Python Dict my_dict[0] = 'sscgeekd.com' my_dict[2] = 'For' my_dict[3] = 1 print(my_dict) # Adding a set of values to the Python Dictionary my_dict['Value_set'] = 2, 3, 4 print(my_dict) # Updating existing Key's Value to the Python Dictionary my_dict[2] = 'Welcome' print(my_dict) # Adding Nested Key value to Dictionary to the Python Dictionary my_dict[5] = {'Nested': {'1': 'Life', '2': 'sscgeeks.com'}} print(my_dict)

Accessing elements of a Python Dictionary

To access the items of a Python dictionary we can use keys inside square brackets.

# Creating a Dictionary in Python
my_dict = {1: 'my_dict ', 'name': 'sscgeeks.com'} # accessing a element by using key print(my_dict ['name']) # accessing a element using key print(my_dict [1])
Output:
sscgeeks.com
 my_dict

Deleting Elements using del Keyword

We can Delete the items in the dictionary by del keyword.

my_dict= {1: 'sscgeeks.com', 'name': 'For', 3:'sscgeeks.com'} print("Dictionary =") print(my_dict)
 #Deleting items in dictionary. del(my_dict[1])  print("Data after deletion Dictionary=") print(my_dict)

Dictionary methods

[wptb id=170]

# Examples of different types of dictionary methods my_dict1 = {1: "Python", 2: "Java", 3: "Ruby", 4: "Scala"} # copy() method my_dict2 = dict1.copy() print(my_dict2) # clear() method my_dict1.clear() print(my_dict1) # get() method print(my_dict2.get(1)) # items() method print(my_dict2.items()) # keys() method print(my_dict2.keys()) # pop() method my_dict2.pop(4) print(my_dict2) # popitem() method my_dict2.popitem() print(my_dict2)
 # update() method my_dict2.update({3: "Scala"}) print(my_dict2) # values() method print(my_dict2.values())

 

 

Leave a comment