Example HTML page

Dictionaries in Python

Dictionaries

A dictionary has key-value pairs. A key in dictionary may be of any Python type but numbers or strings are used usually. Whereas the value can be any object in Python. Dictionaries are enclosed by { }. The values can be assigned and will be accessed by using [ ].

We can access the keys and values by using a dot operator.

Creating Dictionaries

We can create a dictionary and can insert elements into { } braces. This is shown in below.

>>> d={ }                                                                          #Creating an empty dictionary

>>> d

{ }

>>> d={“Day”:”Monday”,”Month”:”July”,”Date”:30}                 #Appending elements into it

>>> d

{‘Date’: 30, ‘Month’: ‘July’, ‘Day’: ‘Monday’}

In the above example day,month and date are known as keys whereas Monday,july and 30 are known as values.

Accessing elements from dictionary

We can access the elements from dictionary as follows. It is illustrated for the same above example.

>>> d={“Day”:”Monday”,”Month”:”July”,”Date”:30}

>>> d

{‘Date’: 30, ‘Month’: ‘July’, ‘Day’: ‘Monday’}

>>> d[‘Month’]

‘July’

>>> d[‘Day’]

‘Monday

>>> d.keys()

dict_keys([‘Date’, ‘Month’, ‘Day’])                                             #Accessing keys

>>> d.values()

dict_values([30, ‘July’, ‘Monday’])                                           #Accessing values

>>> ‘

Updating Dictionary

You can update a dictionary by adding a new entry or a key-value pair, modifying an existing entry, or deleting an existing entry as shown in a simple example given below.

>>> dict={“place”:”Chennai”,”year”:2018,”Day”:”Saturday”}

>>> dict

{‘year’: 2018, ‘place’: ‘Chennai’, ‘Day’: ‘Saturday’}

>>> dict[‘month’]=”July”

>>> dict

{‘year’: 2018, ‘place’: ‘Chennai’, ‘Day’: ‘Saturday’, ‘month’: ‘July’}

>>> dict[“month”]=”June”

>>> dict

{‘year’: 2018, ‘place’: ‘Chennai’, ‘Day’: ‘Saturday’, ‘month’: ‘June’}

Deleting Dictionary elements

We can delete the elements of dictionary by using del statement. Following is a simple example.

>>> d={“Day”:”Monday”,”Month”:”July”,”Date”:30}             #Deleting an element

>>> d

{‘Date’: 30, ‘Month’: ‘July’, ‘Day’: ‘Monday’}

>>> del d[‘Date’]

>>> d

{‘Month’: ‘July’, ‘Day’: ‘Monday’}

Built-in Dictionary functions

Python has the following dictionary functions.

1.len( )

This returns the length of a dictionary.

Ex

>>> dict={“Fruit”:”Apple”,”Color”:”Red”,”Place”:”Himalayas”}

>>> dict

{‘Color’: ‘Red’, ‘Fruit’: ‘Apple’, ‘Place’: ‘Himalayas’}

>>> len(dict)

3

2.str( )

This returns the entire dictionary in string format.

Ex

>>> dict={“Fruit”:”Apple”,”Color”:”Red”,”Place”:”Himalayas”}

>>> dict

{‘Color’: ‘Red’, ‘Fruit’: ‘Apple’, ‘Place’: ‘Himalayas’}

>>> str(dict)

“{‘Color’: ‘Red’, ‘Fruit’: ‘Apple’, ‘Place’: ‘Himalayas’}”          #String format

3.type ( )

This returns the type of variable that we passed into the dictionary.

Ex

>>> dict={“Fruit”:”Apple”,”Color”:”Red”,”Place”:”Himalayas”}

>>> type(dict)

<class ‘dict’>

Built-in dictionary methods

Python has the following dictionary methods.

1.clear( )

This removes all the elements in the dictionary.

Ex

>>> x={“a”:1,”b”:3,”c”:4}

>>> x

{‘c’: 4, ‘b’: 3, ‘a’: 1}

>>> x.clear( )

>>> x

{ }                                     #Displays an empty dictionary after removing the elements

2.copy ( )

This returns another copy of the same existing dictionary.

Ex

>>> x={“a”:1,”b”:3,”c”:4}

>>> y=x.copy()

>>> y

{‘c’: 4, ‘b’: 3, ‘a’: 1}

>>>

3.fromkeys( )

This creates a new dictionary with key elements from the dictionary.

Ex

>>> c={“Name”:”Sudhakar”,”Gender”:”Male”,”Regno.”:1613,”Place”:”Delhi”}

>>> c

{‘Regno.’: 1613, ‘Place’: ‘Delhi’, ‘Name’: ‘Sudhakar’, ‘Gender’: ‘Male’}

>>> d=c.fromkeys(c)

>>> d

{‘Regno.’: None, ‘Place’: None, ‘Name’: None, ‘Gender’: None}

>>> d=c.fromkeys(c,”empty”)

>>> d

{‘Regno.’: ’empty’, ‘Place’: ’empty’, ‘Name’: ’empty’, ‘Gender’: ’empty’}

>>> d=str(c)

>>> d

“{‘Regno.’: 1613, ‘Place’: ‘Delhi’, ‘Name’: ‘Sudhakar’, ‘Gender’: ‘Male’}”

4.keys( ) and values( )

This returns the key elements and the elements in values position in a dictionary.

Ex

>>> c={“Name”:”Sudhakar”,”Gender”:”Male”,”Regno.”:1613,”Place”:”Delhi”}

>>> c

{‘Place’: ‘Delhi’, ‘Regno.’: 1613, ‘Name’: ‘Sudhakar’, ‘Gender’: ‘Male’}

>>> c.keys( )

dict_keys([‘Place’, ‘Regno.’, ‘Name’, ‘Gender’])

>>> c.values( )

dict_values([‘Delhi’, 1613, ‘Sudhakar’, ‘Male’])

5.items( )

This returns the (key,value) pairs in dictionary separately.

Ex

>>> c={“Name”:”Sudhakar”,”Gender”:”Male”,”Regno.”:1613,”Place”:”Delhi”}

>>> c

{‘Place’: ‘Delhi’, ‘Regno.’: 1613, ‘Name’: ‘Sudhakar’, ‘Gender’: ‘Male’}

>>> c.items()

dict_items([(‘Place’, ‘Delhi’), (‘Regno.’, 1613), (‘Name’, ‘Sudhakar’), (‘Gender’, ‘Male’)])

6.update( )

We can update a dictionary by adding a new element or replacing an element with another element into a dictionary.

Ex

>>> c={“Name”:”Sudhakar”,”Gender”:”Male”,”Regno.”:1613,”Place”:”Delhi”}

>>> d={“Regno.”:2616,”Place”:”Mumbai”}

>>> c.update(d)

>>> c

{‘Regno.’: 2616, ‘Name’: ‘Sudhakar’, ‘Gender’: ‘Male’, ‘Place’: ‘Mumbai’}

 

Example HTML page

Leave a Reply

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

Pin It on Pinterest