WeirdGeek

Data Science | Machine Learning | Automation

  • Data Analytics
  • Python
  • Data Science
  • Google Apps Script
  • Machine Learning
  • Artificial Intelligence
  • SQL Server

18/11/2018 By WeirdGeek Leave a Comment

Creating and Computing over Dictionaries in Python

We can define Dictionary as a collection of keys-values pairs which are unordered, mutable and indexable. Dictionaries in Python are written with the use of curly brackets and between curly brackets, we define our key-value pairs separated with colons : .between each pair.  Just like the lists in Python you can manipulate them.

A Python dictionary is a mapping of unique keys to values which means keys should be unique in the dictionary. If you have used two keys with the same name in the dictionary, the last one or the second with the key will be saved.

Creating Dictionaries in Python:
Employee = { 'Name' : 'Mark','Employee_ID' : 15613, 'Status' : 'Active'}
Creating List of Dictionaries in Python:

List of Dictionaries

All_Employees = [

{'Name': 'Mark' , 'Employee_ID': 15613, 'Status': 'Active'},

{'Name': 'Jessica' , 'Employee_ID': 15614, 'Status': 'Active'},

{'Name': 'Robert' , 'Employee_ID': 15615, 'Status': 'Active'}]
Manipulating Dictionary Data

Checking Dictionary Keys:

To find the keys in the dictionary, we can use .keys() method. For example, To check the keys of Employee dictionary that we created above, we can do as:

Employee.keys() dict_keys(['Name', 'Employee_ID', 'Status'])

To check the values in a dictionary:

Employee.values()
dict_values(['Mark', 15613, 'Active'])

Adding a key-value pair in the dictionary in Python:

Employee['Location'] = 'USA'
print(Employee)
Output -
{'Name': 'Mark', 'Employee_ID': 15613, 'Status': 'Active', 'Location': 'USA'}

Checking whether ‘Location’ key is there or not in the Employee, we can write:

'Location' in Employee
Output-True

For Updating a value in the dictionary, we can write:

Employee['Location'] = 'United States'
print(Employee)
{'Name': 'Mark', 'Employee_ID': 15613, 'Status': 'Active', 'Location': 'United States'}

To delete the value in a dictionary in Python, we can use del keyword as shown below:

del(Employee['Location'])
print(Employee)
{'Name': 'Mark', 'Employee_ID': 15613, 'Status': 'Active'}

Basically, DataFrame is a place where we store data using Pandas package. So this is where Dictionaries plays there important part.

Creating Pandas DataFrame using Dictionaries in Python:

First, we’ll create a Dictionary and pass Key-Value pairs. Keep in mind that Key should be unique and only you can define one key for a key-value pair. But for a key, there can be more than one values( if more than one values you want to provide, pass the values in a list) as shown below:

World_Pop = {

'Country' : ['United States of America' , 'United Kingdom', 'Australia', 'India', 'China'],

'Capital' :  ['Washington, D.C.', 'London', 'Canberra', 'New Delhi', 'Beijing'],

'Population in Crores' : [32.57, 6.6, 2.46, 133.92, 138.64]

}
  • Next, import the Pandas package:
import pandas as pd
  • Next, create a DataFrame from a dictionary as
World = pd.DataFrame(World_Pop)

print(World)

Dictionaries in Python using Pandas DataFrame

  • Also if you want to set the index, you can do so by
World.index = ['US', 'UK', 'AU', 'IN', 'CH']
print(World)

Dictionaries in Python using Pandas DataFrame

 

For more information, you can check our post on 35 Pandas codes every data scientist aspirant must know and also the official Python Documentation.

Related posts:

  1. 10 Basic Python fundamentals for Data Scientist aspirants and Data Analysis
  2. Using List and Dictionary Comprehensions in Python
  3. Computing Bootstrap Replicate from Bootstrap Sample using Python
  4. Creating Time Series with Line Charts using Python’s Matplotlib library

Filed Under: Python Tagged With: Dictionaries in Python, Pandas, Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Subscribe to my Blog !!

Enter your email below to subscribe my blog and get the latest post right in your inbox.

  • Home
  • Terms
  • Privacy
  • Contact Us

Copyright © 2025 · WeirdGeek · All trademarks mentioned are the property of their respective owners.