Python Dictionaries

Python Dictionaries

June 14, 2018
5 minutes read time
1 likes
0 comments
351 times viewed


header image

Description

This article will guide you about the use of dictionaries in python. It will explain various functionalities and builtin functions associated with it.

Content

Dictionary

Dictionary Functions & its Description

len(dict):

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

dict = {'Name': 'Manni', 'Age': 7, 'Class': 'First'}
print ("Length : %d" % len (dict))

# Length: 3

str(dict):

Produces a printable string representation of a dictionary

dict = {'Name': 'Manni', 'Age': 7, 'Class': 'First'}
print ("Equivalent String : %s" % str (dict))

# Equivalent String : {'Name': 'Manni', 'Age': 7, 'Class': 'First'}

type(variable):

Returns the type of the passed variable. If passed variable is dictionary, then it would return a dictionary type.

dict = {'Name': 'Manni', 'Age': 7, 'Class': 'First'}
print ("Variable Type : %s" %  type (dict))

# Variable Type : <type 'dict'>

Dictionary Methods & its Description

dict.clear():

Removes all elements of dictionary dict

dict = {'Name': 'Zara', 'Age': 7}
print ("Start Len : %d" %  len(dict))

dict.clear()
print ("End Len : %d" %  len(dict))

# Start Len : 2
# End Len : 0

dict.copy():

Returns a shallow copy of dictionary dict

dict1 = {'Name': 'Manni', 'Age': 7, 'Class': 'First'}
dict2 = dict1.copy()
print ("New Dictionary : ",dict2)

# New dictionary :  {'Name': 'Manni', 'Age': 7, 'Class': 'First'}

dict.fromkeys():

Create a new dictionary with keys from seq and values set to value.

seq = ('name', 'age', 'sex')

dict = dict.fromkeys(seq)
print ("New Dictionary : %s" %  str(dict))

dict = dict.fromkeys(seq, 10)
print ("New Dictionary : %s" %  str(dict))

# New Dictionary : {'age': None, 'name': None, 'sex': None}
# New Dictionary : {'age': 10, 'name': 10, 'sex': 10}

dict.get(key, default=None):

For key key, returns value or default if key not in dictionary

dict = {'Name': 'Zara', 'Age': 27}

print ("Value : %s" %  dict.get('Age'))
print ("Value : %s" %  dict.get('Sex', "NA"))

# Value : 27
# Value : NA

dict.items():

Returns a list of dict’s (key, value) tuple pairs

# Value : [('Age', 7), ('Name', 'Zara')]

dict.keys():

Returns list of dictionary dict’s keys

dict = {'Name': 'Zara', 'Age': 7}
print ("Value : %s" %  dict.keys())

# Value : dict_keys(['Age', 'Name'])

dict.setdefault(key, default = None):

Similar to get(), but will set dict[key] = default if key is not already in dict

dict = {'Name': 'Zara', 'Age': 7}
print ("Value : %s" %  dict.setdefault('Age', None))
print ("Value : %s" %  dict.setdefault('Sex', None))
print (dict)

# Value : 7
# Value : None
# {'Name': 'Zara', 'Sex': None, 'Age': 7}

dict.update(dict2):

Adds dictionary dict2’s key-values pairs to dict

dict = {'Name': 'Zara', 'Age': 7}
dict2 = {'Sex': 'female' }

dict.update(dict2)
print ("updated dict : ", dict)

# updated dict : {'Sex': 'female', 'Age': 7, 'Name': 'Zara'}

dict.values():

Returns list of dictionary dict’s values

dict = {'Sex': 'female', 'Age': 7, 'Name': 'Zara'}
print ("Values : ",  list(dict.values()))

# Values :  ['female', 7, 'Zara']


We are done



Like this post

1   Like


Share this post on

Google Plus LinkedIn


About the author




Join the discussion

Nothing to preview

Post Comment



Comments