Python Dictionaries

⭐️ 👀 2023 WEB DEVELOPMENT BOOTCAMP starting in days! Join the waiting list to reserve your spot in my 10-weeks cohort course and learn the fundamentals, HTML, CSS, JS, Tailwind, React, Next.js and much much more! 👀 ⭐️

Dictionaries are a very important Python data structure.

While lists allow you to create collections of values, dictionaries allow you to create collections of key / value pairs.

Here is a dictionary example with one key/value pair:

dog = { 'name': 'Roger' }

The key can be any immutable value like a string, a number or a tuple. The value can be anything you want.

A dictionary can contain multiple key/value pairs:

dog = { 'name': 'Roger', 'age': 8 }

You can access individual key values using this notation:

dog['name'] # 'Roger'
dog['age']  # 8

Using the same notation you can change the value stored at a specific index:

dog['name'] = 'Syd'

And another way is using the get() method, which has an option to add a default value:

dog.get('name') # 'Roger'
dog.get('test', 'default') # 'default'

The pop() method retrieves the value of a key, and subsequently deletes the item from the dictionary:

dog.pop('name') # 'Roger'

The popitem() method retrieves and removes the last key/value pair inserted into the dictionary:


You can check if a key is contained into a dictionary with the in operator:

'name' in dog # True

Get a list with the keys in a dictionary using the keys() method, passing its result to the list() constructor:

list(dog.keys()) # ['name', 'age']

Get the values using the values() method, and the key/value pairs tuples using the items() method:

# ['Roger', 8]

# [('name', 'Roger'), ('age', 8)]

Get a dictionary length using the len() global function, the same we used to get the length of a string or the items in a list:

len(dog) #2

You can add a new key/value pair to the dictionary in this way:

dog['favorite food'] = 'Meat'

You can remove a key/value pair from a dictionary using the del statement:

del dog['favorite food']

To copy a dictionary, use the copy() method:

dogCopy = dog.copy()

One more thing! ⚠️ ✋

At the end of January I will organize the Web Development Bootcamp.

It's a 10-weeks long cohort online course where I will guide you to becoming a Web Developer.

It's not "just a course". It's a big event I organize once a year.

We'll start from zero, learn the fundamentals of Web Development, HTML CSS, JavaScript, Tailwind, Git, using the command line, VS Code, GitHub, Node.js, we'll then learn React, JSX, how to use PostgreSQL, Astro, Next.js, Prisma, deploying on Netlify/DigitalOcean/Fly/Vercel and much more! 

At the end of the first 10 weeks you'll know how to create web sites and web applications and I'll unlock you the 2nd phase of the Bootcamp: you will get access to a large number of projects exclusive to the Bootcamp graduates, so you can follow my instructions to build things like private areas with authentication, clones of popular sites like Twitter YouTube Reddit, create e-commerce sites, and much much more.

Because once you got the fundamentals, you only learn by working on real, exciting projects.

To find out more, visit