Member-only story

Interview question: What is a dictionary in Python?

Crystal X
2 min readMar 1, 2023

--

A dictionary is a data structure that allows us to write very efficient code. It is a Python implementation of a data structure that is more generally known as an associative array. It consists of a collection of key:value pairs, with each pair mapping the key to its associated value.

A dictionary is a collection of key:value pairs, with the values being mapped to the keys.

A dictionary is defined by enclosing a comma separated list of key value pairs in curly braces ({}) and a colon (:) separating each key from its associated value.

There are two ways to create a dictionary in python:-

  1. Use curly braces, or
  2. Use Python’s inbuilt dict() function.

Create a dictionary using curly braces:

Create a dictionary using Python’s inbuilt dict() function:

Dictionaries are a very fundamental part of machine learning and can be used with the pandas libraries to create dataframes. Below is an example of how to create a dataframe composed of a dictionary that where the values are two arrays of information:

--

--

Crystal X
Crystal X

Written by Crystal X

I have over five decades experience in the world of work, being in fast food, the military, business, non-profits, and the healthcare sector.

No responses yet