Python Dictionaries with program examples

Spread the love

Python Dictionaries:

It is unordered data structure with key and values. It defined with curley braces. We can create, edit, update and delete the values. We can store integer, float, strings and complex numbers.

How to Create a Empty Dictionary:

Create an empty dictionary with {} braces.

sample={} print(sample)

Output:

{}

How to Create a Dictionary:

We can also create a dictionary with dict function in python with 3 ways. Here are those.

Example 1:

sample=dict({'a':10,'b':20}) print(sample)

Output:

{'a': 10, 'b': 20}

Example 2:

sample=dict(a=10,b=20) print(sample)

Output:

{'a': 10, 'b': 20}

Example 3:

sample=dict([('a',10),('b',20)]) print(sample)

Output:

{'a': 10, 'b': 20}

How to Create of dictionary with Integer numbers:

We can create integer dictionaries by simply passing integer values to it.

int_dic={'a':10,'b':20,'c':30,'d':40} print(int_dic)

Output:

{'a': 10, 'b': 20, 'c': 30, 'd': 40}

How to Create of dictionary with Float numbers:

We can create float  dictionaries by simply passing float values to it.

float_dic={'a':10.5,'b':20.6,'c':30,'d':40} print(float _dic)

Output:

{'a': 10.5, 'b': 20.6, 'c': 30, 'd': 40}

Mixed Datatypes Dictionary:

We can create dictionary with float, integer and strings.

Example Program code:

student={'age':20,'sex':'male','height':6.5} print(student)

Output:

{'age': 20, 'sex': 'male', 'height': 6.5}

Nested dictionary:

We can create a nested dictionary a dictionary with in the dictionary.

nested = {'a':{'age': 20, 'sex': 'male', 'height': 6.5}, 'b': {'age': 21, 'sex': 'male', 'height': 6.5} } print(nested) print(nested['b']['age'])

Output:

{'a': {'age': 20, 'sex': 'male', 'height': 6.5}, 'b': {'age': 21, 'sex': 'male', 'height': 6.5}}

21

How to accessing elements from dictionary:

We can access elements from dictionaries by using key for value.

Example Program code:

student={'age':20,'sex':'male','height':6.5} print(student['sex'])

Output:

Male

How to add elements to dictionary:

We can add easily elements to dictionary by specifying key and value.

Example Program code:

student={'age':20,'sex':'male','height':6.5} print(student) student['salary'] =20000 print(student)

Output:

{'age': 20, 'sex': 'male', 'height': 6.5}

{'age': 20, 'sex': 'male', 'height': 6.5, 'salary': 20000}

How to edit or Update elements to dictionary:

We can edit or update the values same like adding values with specifying key and values.

Example Program code:

student={'age':20,'sex':'male','height':6.5} print(student) student['age'] =21 print(student)

Output:

{'age': 20, 'sex': 'male', 'height': 6.5}

{'age': 21, 'sex': 'male', 'height': 6.5}

How to delete elements from dictionary

We can delete elements from dictionaries using del command or pop .

Example program code:

student={'age':20,'sex':'male','height':6.5} print(student) del student['age'] print(student)

Output:

{'age': 20, 'sex': 'male', 'height': 6.5}

{'sex': 'male', 'height': 6.5}

By using pop function

We can delete dictionary elements by using pop. We can call with below syntax

Syntax:  dictionary. pop(indexvalue)

Example program code:

student={'age':20,'sex':'male','height':6.5} print(student) student.pop('sex') print(student)

Output:

{'age': 20, 'sex': 'male', 'height': 6.5}

{'age': 20, 'height': 6.5}

By using popitem function.

This function automatically removes the last item from the dictionary.

Syntax: Dictionary.popitem()

Example program code:

student={'age':20,'sex':'male','height':6.5} print(student) student.popitem() print(student)

Output:

{'age': 20, 'sex': 'male', 'height': 6.5}

{'age': 20, 'sex': 'male'}

How to check the length of the dictionary:

We can check the length of the dictionary by using len() function.

Example program code:

student={'age':20,'sex':'male','height':6.5} print(len(student))

Output:

3

 

admin

admin

Leave a Reply

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