x

Python Dictionary

PREV    NEXT

  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:

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:

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:

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:

Output:

Clear:

clear is method to delete the all elements in the dictionary

Syntax:

dictionary_name.clear()

Example:

Output:

Len method:

This method is used to find the length of the dictionary


Syntax:

len(dictionary_name)

Example:

Output:

Iterate the dictionary by using the keys ,like below

Example:

Output:

Membership operator:

Example:

Output:

Relation operators with dictionary

 we can compare the tw dictionaries by usin relational operator

Example:

Output:

Keys Method:

 To extract the keys we can use keys method

Syntax:

dictionary_name.keys()

Example:

Output:

values Method:

 To extract the values we can use keys method

Syntax:

dictionary_name.values()

Example:

Output:

Items Method:

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

Syntax:

dictionary_name.items()

Example:

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:

Output:

pop method:

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

Syntax:

dictionary_name.pop(‘existingkey’)

Example:

Output:

Popitem method:

This method delete the random elements and return thet element

Syntax:

dictionary_name.popitem()

Example:

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:

Output:

PREV    NEXT



Like it? Please Spread the word!