Dictionaries in python

 Dictionaries are used to store data values in keyvalue pairs.

A dictionary is a collection which is ordered, changeable and does not allow duplicates.

thisdict = {  "brand": "Ford",  "model": "Mustang",  "year": 1964}


PART ONE

print(thisdict)   print a dictionary

print(thisdict["brand"])     Print the "brand" value of the dictionary

Dictionaries cannot have two items with the same key.

print(len(thisdict)) To determine how many items a dictionary has, use the len() function

The values in dictionary items can be of any data type

print(type(thisdict)) From Python's perspective, dictionaries are defined as objects with the data type 'dict'     <class 'dict'>


Accessing Items

x = thisdict["model"]   You can access the items of a dictionary by referring to its key name, inside square brackets

x = thisdict.get("model")  There is also a method called get() that will give you the same result

x = thisdict.keys() The keys() method will return a list of all the keys in the dictionary.

x = thisdict.items()  The values() method will return a list of all the values in the dictionary.

if "model" in thisdict:   To determine if a specified key is present in a dictionary use the in keyword

Changing Items

thisdict["year"] = 2018     You can change the value of a specific item by referring to its key name

thisdict.update({"year": 2020})   The update() method will update the dictionary with the items from the given argument.

Adding Items

thisdict["color"] = "red"      Adding an item to the dictionary is done by using a new index key and assigning a value to it:

thisdict.update({"color": "red"})   The update() method will update the dictionary with the items from a given argument. If the item does not exist, the item will be added.

Removing Items

thisdict.pop("model")   The pop() method removes the item with the specified key name:

thisdict.popitem()  The popitem() method removes the last inserted item
del thisdict["model"]   The del keyword removes the item with the specified key name

del thisdict    The del keyword can also delete the dictionary completely:

thisdict.clear()  The clear() method empties the dictionary:


Loop in Dictionary 

for x in thisdict:
  print(x)      Print all key names in the dictionary, one by one

for x in thisdict:
  print(thisdict[x])      Print all values in the dictionary, one by one

for x in thisdict.values():
  print(x)        You can also use the values() method to return values of a dictionary:

for x in thisdict.keys():
  print(x)      You can use the keys() method to return the keys of a dictionary:

for x, y in thisdict.items():
  print(x, y)      Loop through both keys and values, by using the items() method:

Copy in Dictionary 

You cannot copy a dictionary simply by typing dict2 = dict1, because: dict2 will only be a reference to dict1, and changes made in dict1 will automatically also be made in dict2.

There are ways to make a copy, one way is to use the built-in Dictionary method copy().

mydict = thisdict.copy()

mydict = dict(thisdict))



PYTHON PROGRAM SYNTAX SERIES

Comments

Popular Posts