Member-only story
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:-
- Use curly braces, or
- 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: