Saturday, 17 August 2019

Python dictionary questions with answers | Python dictionary examples with solution


Python 3 dictionary questions with answers | Python 3 dictionary examples with solution


Accessing Keys or values simultaneously


To access all the keys in a dictionary at once, we use keys () method as dictionary-name. Keys (). If you want to see all values at once. We use values () method as dictionary-name. values ().

For example:

Dictvowel= {“vowel1”: “u”, “vowel2”: “e”, “vowel3”: “o”, “vowel4”: “i”,}
Dictvowel. keys ()
Dictvowel. values ()

Screenshot:

Key() function dictionary in python


The keys () method returns all the keys defined in a dictionary in the form of a sequence.
The values () method returns all the values defined in that dictionary in the form of sequence.

Keys must be Unique in Dictionary in Python


In a dictionary, each of the keys must be unique. There cannot be duplicate keys in a dictionary. But one key can have more than one value and two unique keys can have same values.

For Example:

BirdCount= {“Finch”:”10”,” Myna”:”13”,” Hornbill”:”15”,” Peacock”:”15”}
Here two different keys “Hornbill” and “Peacock” have same value 15.

But if you try to add same key in a dictionary. It will overwrite it.

Let’s see

Example of python dictionary with answer


Here we are trying to add a new key with same name that is already exists. Then it is overwritten by Python.


Mutable Dictionary


Like Lists, Dictionaries are also mutable. We can change the value of a certain key using assignment operator.

Syntax:

Dictionary-name [Key]= value

For Example

BirdCount= {“Finch”:”10”,” Myna”:”13”,” Hornbill”:”15”,” Peacock”:”15”}
BirdCount[“Myna”] = 14

Screenshot:



Multiple ways of Creating Dictionaries

There are 3 ways to create a Dictionary in Python.

1.In first method, all the key: value pairs in curly braces separated by commas.

For example:

Employee= {“name”: “Harsh”,” Salary”: “40000”, “age”: “29”}

2.In second method, adding key: value pairs to an empty dictionary.

Create an empty dictionary then add key: value pairs

Employee= {}

Employee[“name”] = “Harsh”

Employee[“Salary”] = 40000

Employee[“age”] = 29

3.In third method, create a dictionary using dictionary constructor dict ().

Employee= dict (name=’Harsh’, salary=40000, age=29)

Or

Employee= dict ({‘name’:’Harsh’,’salary’:’40000’,’age’:’29’})

Or

Employee=dict ([[‘name’, ‘Harsh’], [‘salary’,’40000’], [’age’,’29’]])

Here is one list type argument passed to dict () constructor. This list argument in turn contains all key: value pairs as lists. (3 list type entries in one list).

Or

Employee=dict (([‘name’, ‘Harsh’], [‘salary’,’40000’], [’age’,’29’]))

Here the dict () constructor is provided tuple argument. The passed tuple contains list-elements of keys, values (3 list entries in one tuple).

Or

Employee=dict (((‘name’, ‘Harsh’), (‘salary’,’40000’), (’age’,’29’)))

Here the dict () constructor is provided tuple argument. The passed tuple contains tuple-elements of keys, values (3 tuple entries in one tuple).

The zip () Function In dictionary

The zip () function clubs the First value from first set with the First value of second set, second value from first set with the second value of second set, and, so on.

Example= dict (zip ((‘name’,’salary’,’age’), (‘Harsh’,40000,29)))

Screenshot:
How to use zip function in python


Here ‘name’ is clubbed with ‘Harsh’, ‘salary’ with 40000 and ‘age’ with 29. Using these clubbed values, the dict () constructor creates key: value pairs for the dictionary.






No comments:

Post a Comment

Recent Post

Python Project | Banking System | Project on Banking System using python

  PYTHON PROJECT Banking System import csv import pandas as pd import matplotlib.pyplot as plt import sys import datetime found=False def ne...