Dictionary

Dictionary

- Dictionaries are the data types.

- Dictionaries are used to store multiple data in a single variable.

- Dictionaries are created using curly brackets {}, with keys and values.

- Dictionaries are changeable.
 

- Dictionaries are NOT allowed duplicate values.

- We can change, add, or remove in a dictionary.

- Dictionaries are indexed (access) by their keys.


Dictionary:

- "name", "work", "age" is the keys

- "vimdhayak ji", "nothing", 60 is the values. (In the Below Code)



Dictionaries are Not Allowed Duplicates Elements:


Output:-



Dictionary type():

- The type() method Returns the datatype of cheems_dict. (In the below code)

Syntax: type(variable_name)



Dictionary len():

len() stands for length.

- The len() method Returns the length of cheems_dict. (In the below code)

Syntax: len(variable_name)



Dictionary Specific Single Value:

- In the dictionary, If we want to print the value of a key in a dictionary then we use the "work" key to print their "nothing" value. (In the below code)

Syntax: variable_name["Key of dictionary"]



Change Value in Dictionary:

This is the simple method to change the key's value in a dictionary.

- We change the name by using the "name" key. (In the below code)

Syntax: variable_name["Key of dictionary"] = "New Value"


Change Value in Dictionary using update() method:

- The update() method is used to change the key's value in a dictionary.

We change the age by using the "age" key. (In the below code)

Syntax: variable_name.update({"Key of Dictionary": "New Value"})



Add New Elements in Dictionary using update() method:

- The update() method is used to add new elements in a dictionary.

Syntax: variable_name.update({"New Key": "New Value"})


Add New Elements in Dictionary:

- This is the simple method to add new elements in a dictionary.

Syntax: variable_name["New Key"] = "New Value"


Dictionary pop():

- The pop() method is used to remove the specific element by their key in a dictionary.

Syntax: variable_name.pop("Key of Dictionary")



Dictionary popitem():

- The popitem() method is used to remove the last element in a dictionary.

Syntax: variable_name.popitem()



Dictionary clear():

- The clear() method is used to clear all the elements in a dictionary.

- Output: {}.

Syntax: variable_name.clear()



Dictionary del:

- del stands for delete.

- del used to delete a dictionary and return nothing.

- del is also used to delete the specific element in a dictionary by their key.


Delete a Dictionary:

Syntax: del variable_name



Delete the specific element in a Dictionary by their key:

Syntax: del variable_name["Key of Dictionary"]


Comments