Python Dictionaries

A Python dictionary is a built in data type used to store key-value pairs.

It is similar to a real dictionary where you have to look up a word (key) to find its defintion (value)

In Python,  a dictionary stores data in linked key-value pairs

Each key is connected to a specific value, making it easy to look up data by name rather than by position.

Keys must be unique i.e you cannot have two items with the same key.

Values can be anything e.g strings, numbers, lists and even other dictionaries

  {'Key':'Value'}
  coder = {'name': 'Ryan', 'age': '26', 'languages': ['SQL', 'Python']}

we use the .keys() method to show all the keys in the dictionary

  print(coder.keys())

We use the .values() method to show the values of the dictionary.

  print(coder.values())

We usethe .items() to return a view object that displays all the key value pairs in the dictionary as tuples.

  print(coder.items())
  print(coder)

Here we access the value associated with the key ‘name’ in the coder dictionary.

in a dictionary, we use dict[key] to retrieve the value linked to that specific key.

  print(coder['name'])

Here, we loop through the values in the coder dictionary.

   for data in coder.values(): print(data)
   for key, value in coder.items(): print(key, ':', value)

Lets talk about the .get() method.

The .get() method in Python is used to safely retrieve a value from a dictionary using a key.

  print(coder.get('name'))
  print(coder.get('company', 'Not Found'))

Create a new key

This adds a  new key-value pair to the coder dictionary.

If the key ‘company’ does not exist, Python will create it and assign it the value ‘fintech’

if it already exists, the value will be updated or overwritten.

  coder['company'] = 'fintech'
update method

.update() method is used to modify existing keys or add new key-value pairs in a dictionary.

  coder.update({'name': 'nolan', 'languages': ['SQL', 'Python', 'html', 'css']})
  print(coder)
delete

We use the del statement to remove the key-value pair with the key ‘company’ from the coder dictionary.

  del coder['company']
pop

pop removes from the dictionary but stores in variable.

  languages = coder.pop('languages')
  languages
  print(len(coder))

Here we create a dictionary using the built-in dict() constructor.

The keys and values are passed as keyword arguments (team=”Yankees”, name=”Ruth”)

  baseball_player = dict(team = "Yankees", name = 'Ruth')
  print(baseball_player)
memory

bb2 = baseball_player, creates a reference to the same dictionary object, not a copy.

so when we update bb2, we are also changing baseball_player

Â

  bb2 = baseball_player

Here we add “division”: “east” to the dictionary

  bb2['division'] = 'east'
  bb2
  baseball_player

Here we create a copy of the baseball_player dictionary using the .copy() method.

Hence what ever chnages we make to the copy, does not affect the original.

  bb3 = baseball_player.copy()
  bb3['era'] = '1920s'
  baseball_player
  bb3

We use the .clear() method to remove all key-value pairs from the dictionary.

  bb3.clear()
  print(bb3)
  baseball_player.setdefault('sport', 'baseball')
  baseball_player['sport']
From Keys to make a dictionary
  teams = ['Dodgers', 'Giants', 'Rockies'] wins = 0

we use the .fromkeys(keys, value) to create a new dictionary with each item in the keys list as a key, and we set them all to the same value.

  west = dict.fromkeys(teams, wins)
  print(west)

Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.

Leave a Reply

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