Python Dictionary (With Examples)

Python Dictionary

dictionary in python


Dictionary is an ordered set of a key-value pair of items. They work like associative arrays or hashes and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object. 

Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]).

Example:

d = {1:'abc'2:'xyz'3:'pqr'}
print(d[1])
print(d[3])
print (d)
print (d.keys())
print (d.values())

Output:

abc
pqr
{1'abc'2'xyz'3'pqr'}
dict_keys([123])
dict_values(['abc''xyz''pqr'])


Accessing Dictionary Values 

Dictionaries values can be accessed by using the square braces, i.e. [ ] along with the key name to obtain the value. This is multiple item declaration procedures used to declare keys along with their values in Python's dictionary.

Example:

d = {1:'abc'2:'xyz'3:'pqr'}
print(d[1])
print(d[3])
print (d)

Output:

abc
pqr
{1'abc'2'xyz'3'pqr'}


Update Dictionary 

Programmers can update or modify the existing dictionary by simply adding a new entry or a key-value pair or by deleting an item or entry.

Example:

d = {1:'abc'2:'xyz'3:'pqr'}
#update dictionary
d[1]='hi'
print (d)
#add element into dictionary
d['name'] = 'jack'
d[4] = 'how'
print(d)

Output:

{1'hi'2'xyz'3'pqr'}
{1'hi'2'xyz'3'pqr''name''jack'4'how'}


Deleting Python 

Dictionary Elements del statement is used for performing deletion operations. An item can be deleted from a dictionary using the key only.

Example:

d = {1:'abc'2:'xyz'3:'pqr'}
del d[1]
print (d)

Output:

{2'xyz'3'pqr'}

Deleting full dictionary:

Example:


#delete full dictionary
del d
print(d)
#print error dictionary is deleted

Output:

Error


len(dict) Method 

Gives the total length of the dictionary. This would be equal to the number of items in the dictionary.

Example:

d = {1:'abc'2:'xyz'3:'pqr'}
print(len(d))

Output:

3


str(dict) Method 

This method returns string formation of the value.

Example:

d = {1:'abc'2:'xyz'3:'pqr'}
print(str(d))

Output:

{1'abc'2'xyz'3'pqr'}


clear() Method 

Removes all the elements from the dictionary.

Example:

d = {1:'abc'2:'xyz'3:'pqr'}
d.clear()
print (d)

Output:

{}


copy() Method 

Returns a copy of the dictionary.

Example:

d = {1:'abc'2:'xyz'3:'pqr'}
d1=d.copy()
print (d1)

Output:

{1'abc'2'xyz'3'pqr'}


get() Method 

Returns the value of the specified key.

Example:

d = {1:'abc'2:'xyz'3:'pqr'};
print(d.get(1))

Output:

abc


items() Method 

Returns a list containing the tuple for each key-value pair.

Example:

d = {1:'abc'2:'xyz'3:'pqr'}
print(d.items())

Output:

dict_items([(1'abc'), (2'xyz'), (3'pqr')])


keys() Method 

Returns a list containing the dictionary's keys.

Example:

d = {1:'abc'2:'xyz'3:'pqr'};
print(d.keys())

Output:

dict_keys([123])


pop() Method 

Removes the element with the specified key.

Example:

d = {1:'abc'2:'xyz'3:'pqr'};
print(d.pop(1))
print (d)

Output:

abc
{2'xyz'3'pqr'}


popitem() Method 

Removes the last inserted key-value pair.

Example:

d = {1:'abc'2:'xyz'3:'pqr'}
print(d.popitem())
print (d)

Output:

(3'pqr')
{1'abc'2'xyz'}


update() Method 

Updates the dictionary with the specified key-value pairs.

Example:

d = {1:'abc'2:'xyz'3:'pqr'};
d.update({4:'hi'})
print (d)
d.update({2:'how'})
print (d)

Output:

{1'abc'2'xyz'3'pqr'4'hi'}
{1'abc'2'how'3'pqr'4'hi'}


values() Method 

Returns a list of all the values in the dictionary.

Example:

d = {1:'abc'2:'xyz'3:'pqr'};
print(d.values())

Output:

dict_values(['abc''xyz''pqr'])

Next Post Previous Post
No Comment
Add Comment
comment url