python dictionary

What is dictionary?

A group of objects are represented as key-value pairs in a data structure is known as dictionary.

Basic Syntax

dic_name = {key : values}

Important points:

  • Indexing and slicing not work.
  • Insertion order is preserved.
  • Heterogenous elements are allowed
  • Mutable nature.
  • Key must be unique, but duplicates values are allowed.

Access Dictionary:

You can use Python's print() function and the dictionary as an argument to print a dictionary.

For Example:

dict = {
id : 101,
"name" : "Arsalan",
"field" : "Data Analytics"
}
print(dict)

Output is : 

{101, 'name': 'Arsalan', 'Field': 'Data Analytics'}

You can also print some specific values from the dictionary by using the key associated with them.

For Example:

dict = {
id : 101,
"name" : "Arsalan",
"field" : "Data Analytics"
}
print(dict[id])

Output is : 

Arsalan

An alternative mechanism for getting values out of a dictionary is the get() method. If a key is provided but does not exist a default value is returned instead.

For Example:

dict = {
id : 101,
"name" : "Arsalan",
"field" : "Data Analytics"
}
print(dict.get("name"))

Output is : 

Arsalan

Keys Method:

Dictionary objects in python have a built-in method known as keys() function that returns a view object containing the dictionary keys. To get a list of keys in a dictionary use the keys() method.

For Example:

dict = {
id : 101,
"name" : "Arsalan",
"field" : "Data Analytics"
}
print(dict.keys())

Output is : 

dict_keys([id, 'name', 'Field'])

Values Method:

In Python, the values() method is a built-in function of dictionary objects that returns a view object that contains the values of the dictionary. The values() process can be used to retrieve a list of values in a dictionary.

For Example:

dict = {
id : 101,
"name" : "Arsalan",
"field" : "Data Analytics"
}
print(dict.values())

Output is : 

dict_values([101, 'Arsalan', 'Data Analytics'])

Add Elements:

You can add elements to a dictionary in Python by using the square bracket notation. A key value pairs can be added to a dictionary by simply putting the name of the key within square brackets and giving it a values.

For Example:

dict = {
id : 101,
"name" : "Arsalan",
"field" : "Data Analytics"
}
dict["city"] = "Karachi"
print(dict)

Output is : 

{id: 101, 'name': 'Arsalan', 'Field': 'Data Analytics', 'city': 'Karachi'}

Change Elements:

You can change the value of an existing key in a Python dictionary by using the square bracket notation to access the key and then assigning it a new value.

For Example:

dict = {
id : 101,
"name" : "Arsalan",
"field" : "Data Analytics"
}
dict["name"] = "Arsalan Khatri"
print(dict)

Output is : 

{id: 101, 'name': 'Arsalan Khatri', 'Field': 'Data Analytics', 'city': 'Karachi'}

Remove Elements:

1). del Method:

To remove a specific key-value pair from a dictionary using the del statement, you can specify the key to be deleted in square brackets.

For Example:

dict = {
id : 101,
"name" : "Arsalan",
"field" : "Data Analytics"
}
del dict[id]
print(dict)

Output is : 

{'name': 'Arsalan', 'Field': 'Data Analytics'}

2). pop() Method:

The pop() method allows you to remove a specific key-value pair from a dictionary by passing the key to be deleted as an argument:

For Example:

dict = {
id : 101,
"name" : "Arsalan",
"field" : "Data Analytics"
}
dict.pop(id)
print(dict)

Output is : 

{'name': 'Arsalan', 'Field': 'Data Analytics'}

Full Detailed Video 👇👇👇


LIKE SHARE AND SUBSCRIBE