You are on page 1of 9

DICTIONARIES IN PYTHON

1. What is Python dictionary?

Ans, Python Dictionary is an in-built collection data type that does not contain duplicates
generally known as an associative array. It is an ordered collection( As of Python
version 3.7, dictionaries are ordered. But in Python version 3.6 and earlier, dictionaries
are unordered).

A dictionary consists of a collection of key-value pairs. Each key-value pair maps the
key to its associated value. Dictionary is listed in curly brackets, inside these curly
brackets, keys and values are declared. Each key is separated from its value by a colon
(:), while commas separating each element.

Dictionary contains unique keys, but values can be duplicated. We can’t use the index
to access the dictionary element else use the keys because Dictionary is indexed by
keys.

2. What are different Properties of Python Dictionary?

Ans. Dictionaries are mutable, which means that we can edit, add or remove items after the
dictionary has been created.

 Dictionaries can be nested into each other, which means a dictionary can contain
another dictionary.
 Dictionary elements can be accessed by using keys.
 Dictionaries hold the data as key-value pairs. For each key, we have a value that
makes the dictionaries very easy to access.
 Dictionary keys do not allow polymorphism means it allows only unique keys.
Dictionaries do not allow two items with the same key. All key has to be unique.
 Dictionary keys are case sensitive, which means you can use the same key with a
different case and they will be treated as different keys. For example, name and
NAME are two different keys.
 Dictionaries are unordered and their data elements are not accessible in a defined
order, this is the reason we cannot use indexing to access dictionary elements. We
need to use keys to access dictionary elements.
 The data elements in the dictionary can be of any data type. You can have data of
any basic or user-defined data types in dictionary-like strings, int, boolean, lists, etc.

3. How to create a Python dictionary?

Ans. In Python two ways to create Dictionary in Python

 Using curly braces{ }


 Using dict() constructor

4. What is dict( ) in Python dictionary?

Ans. The dict( ) is a constructor to create a dictionary. It does not return anything.

Syntax

Keyword_argument*: Any number of (key : value,……..key : valueN) arguments can be


passed separated by comma.
5. How to use curly braces({ }) to Create Dictionary?

Ans. In this example we are using the curly braces {} from to create a Python dictionary with
integer and string keys

Create Dictionary with integer keys

6. How to update item in Python Dictionary?

Ans. The Items in the python dictionary can be added/append key-values pair using these
ways.
 Using Update() Method : Dictionary update() method update key’s value if already
exits else add new key-value pair.

7. How to get all keys of dictionary?

Ans. To get all keys of the Python dictionary inbuilt keys() method is used.Let understand
with an example

8. How to get all values of Python dictionary?

Ans. To get all values of the python dictionary inbuilt values( ) method is used. Let
understand with an example
9. How to get Python dictionary all key-values pair?

Ans. We can use Python dictionary inbuilt items() method to get all key-value pairs of a
dictionary.

10. How to loop over python dictionary?

Ans. For loop : To access value by key of Python dictionary


11. How to access items of Python dictionary?

Ans.

 To access an item of dictionary we can use the get() method


 To access an item of the dictionary we can access a key name.

12. How to update a Python Dictionary?

Ans. To update a Python dictionary there are two ways:

 Using the key name


 Using the dictionary update( ) method
Using key name
Update dictionary using update() method

The update() method updates the Python dictionary with the items pass as arguments.

The is mandatory argument must be a dictionary, or an iterable object with key: value
pairs.

13. How to remove item from Python dictionary?

Ans. There are many ways to delete elements from Python Dictionary

 pop( ): It removes the with the specified key name.

 popitem( ): It deletes the last inserted item.

 del: It deletes the item with the specified key name or it deletes the

dictionary completely if the key name is not passed

 clear( ) : It empties the dictionary


Remove item Using Dictionary Pop( ) Method
EXTRA QUESTIONS

1. How to use dictionary comprehension with condition?

Ans. We can add single or multiple condition in dictionary comprehension. Using the
python if statement let us understand how to achieve this using example.

Single condition with dictionary comprehension

2. What is Python dictionary copy( ) method?

The Python dictionary copy( ) method return a copy of existing dictionary.

You might also like