Python Dictionary

  1. In this tutorial we can learn about python dictionary, it is one of the most important concept in python programming language
  2. Dictionaries are mutable values,it means we can add,delete,update the values according to our requirement
  3. Values are not stored in sequence order
  4. Dictionary contains two values key and value ,which is separate by ‘:’
  5. Dictionaries created by using curly braces {}
  6. In Dictionary keys are always unique and vules might be can repeated
  7. We can traverse the dictionary by using the key using esily

Synatax:                                                                

Dictionary_Name={‘key1′:value1,’key2′:value2,’key3′:value3,’key4′:value4 ……………’keyn’:valuen}

Access The Dictionary:

Dictionary_Name[‘key1’]

Dictionary_Name[‘key2’]

………………………………….

…………………………………


………………………………….

Dictionary_Name[‘keyn’]

Note: We can create the empty dictionary also

Dictionary_Name={}


Example:

COMPILE & RUN

Output:

 

  • The above example is creating the dictionary of 5 elements.In each element first string is a key and second string is value i.e ramu is the key for the value of 1234567890,so rest of all also created in same manner
  • We can create the dictionary by using predefined method “dict()”,by using this method we can create the dictionary.But here we can’t use use like key and pairs,we can pass the keyword arguments

Syntax:

Dictionary_Name=dict()

Example:

COMPILE & RUN

Output:

 

If particular key not found in dictionary then it throughs the error is  as ‘Keyerror’

Traceback (most recent call last):

  File “dict.py”, line 12, in <module>

    print (“phone number:”,phone_book[‘vnod’])

KeyError: ‘vnod’

 We can create the dictionay combination of multiple datat types i.string,integers,floats etc

Example:

COMPILE & RUN

Output:

 

Add ,update and delete  the elements the existing dictionary

Add:

We add the new element to the alraedy existing dictionary

Syntax:

dictionary_name[‘Nekey’]=value

Update:

We can update already exsting  element to the alraedy existing dictionary

Syntax:

dictionary_name[‘existingkey’]=value

delete:

By using delete we can delete individual element from the dictionary

Syntax:

del dictionary_name[‘existingkey’]

Example:

COMPILE & RUN

Output:

Before modifyiing the details

*********************************

 

Traceback (most recent call last):

  File “dict2.py”, line 47, in <module>

    print (“Skill is:”,student_details[‘skill’])

KeyError: ‘skill’

Clear:

clear is method to delete the all elements in the dictionary

Syntax:

dictionary_name.clear()

Example:

COMPILE & RUN

Output:

Before delete  the details

*********************************

 

After delete  the details

*********************************

Traceback (most recent call last):

  File “dict3.py”, line 19, in <module>

    print (“Name is:”,student_details[‘name’])

KeyError: ‘name’

len method:

This method is used to find the length of the dictionary

Syntax:

len(dictionary_name)

Example:

COMPILE & RUN

Output:

 

Example:

COMPILE & RUN

Output:

 

Membership operator:

Example:

COMPILE & RUN

Output:

 

Relation operators with dictionary

 we can compare the tw dictionaries by usin relational operator

Example:

COMPILE & RUN

Output:

 

Keys Method:

 To extract the keys we can use keys method

Syntax:

dictionary_name.keys()

Example:

COMPILE & RUN

Output:

 

values Method:

 To extract the values we can use keys method

Syntax:

dictionary_name.values()

Example:

COMPILE & RUN

Output:

 

Items Method:

It will return the sequence of tuples,in each tuple key and value will contain

Syntax:

dictionary_name.items()

Example:

COMPILE & RUN

Output:

 

Get Method:

Get method also one of the method to get the specific value by using key ,while get we found specific in dictionary then it will return the value or else it will return the None,But when we are using key we can mentione the default statement also

Syntax:

dictionary_name.get(key,[default])

Example:

COMPILE & RUN

Output:

 

pop method:

pop method is used delete the particular element by using the key.

Syntax:

dictionary_name.pop(‘existingkey’)

Example:

COMPILE & RUN

Output:

 

Popitem method:

This method delete the random elements and return thet element

Syntax:

dictionary_name.popitem()

Example:

COMPILE & RUN

Output:

 

Note: It will delete and returns the random value

Copy method:

it  will create the new dictionary

Syntax:

copy_dictionary_name=dictionary_name.copy()

Example:

COMPILE & RUN

Output:

After copied the details

*********************************

 

Share this website !!!
Facebook Twitter Linkedin