Dictionary & Sets in Python

In Python dictionaries are used to store data in key:value pair, a dictionary is unordered, changeable, and does not allow duplicate keys in it a simple dictionary example is given below.

the_dict =  {
   "name": "xyz",
   "age": 29,
   "city": "New York"
 }

Items in Dictionary

In dictionary each item stored in key:value pair, every key in dictionary is unique mean if you repeat key in dictionary it will override previous key’s value in dictionary. For example

thisdict = {
   "brand": "Ford",
   "model": "Mustang",
   "year": 1964,
   "year": 1974
 }
 print(thisdict["brand"])
In this code if you will try to get result of year it will return 1974, instead of 1964

Unordered & Changeable

Unordered mean you can not access a dictionary item by index. Dictionary item can changed any time once its defined.

Data Types

Dictionary supports all kind of data types like you can store integers, string, float, list etc. in single dictionary with its proper key name

the_dict = {
   "name": "xyz",
   "city": "New York",
   "year": 1964,
  "hobbies": ["Cricket", "Reading"]
 }  

Access Item from Dictionary

An item can be accessed from dictionary in different ways. You can access value by using square brackets and key name like

the_dict = {
   "name": "xyz",
   "city": "New York",
   "year": 1964,
  "hobbies": ["Cricket", "Reading"]
 }  

x = the_dict["name"]

This code will store xyz in variable x . There is also a method available get() for getting a value. LIKE

x = the_dict.get("name")

This will give same result but the only difference is that above procedure will give an error if key not exist in dictionary and get() will return None.

Change Items

Item can change by using assignment operator like

the_dict= {
   "name": "xyz",
   "city": "New York",
   "year": 1964,
  "hobbies": ["Cricket", "Reading"]
 }  

the_dict["name"] = "abc"

This code will change value of name in the dictionary to abc. There is also a function update() to change value of item.

the_dict.update({"year":1974})

This function will update the value of year in dictionary.

Dictionary Functions

  • the_dict.keys() will return all keys
  • the_dict.values() will return all values as list
  • the_dict.items() will return each item in dictionary as a tuple in list
  • in keyword determine that if a key exist in dictionary or not
  • the_dict.pop(“name”) will remove item having key name
  • del the_dict[“name”] will also remove item having key name
  • del the_dict will remove complete dictionary
  • the_dict.clear() empties the dictionary

Nested Dictionary

A dictionary can also be nested mean one dictionary can also contain more dictionaries

family = {
  "child1" : {
    "name" : "Jon",
    "birthYear" : 2004
  },
  "child2" : {
     "name" : "Tobias",
    "birthYear" : 2007
  },
     "child3" : {
    "name" : "Linus",
     "birthYear" : 2011
  }
}

Sets

Sets are used to store multiple values in one variable, but set is a collection which is unordered and unindexed mean you can not access its value by using index or key but you can check if it a value exist in set or can loop through sets.

How to define

A set can be defined by curly braces like

the_set = {"Apple", "Banana", "Cherry"}

Duplicate values are not allowed in sets it will keep only one value if duplicate values are entered. set() Constructor also use to define a set like

the_set = set(("apple", "banana", "cherry"))
tropical = {"pineapple", "mango", "papaya"}

Functions of Sets

  • the_set.add(“orange”) will add orange in set
  • thisset.update(tropical) will add element from tropical to the_set
  • thisset.remove(“banana”) will remove banana from set
  • thisset.discard(“banana”) will also remove banana from set but it will not give any error if banana not exist in set remove() will give error if item not exist in set
  • thisset.clear() empties the set

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s