Pular para o conteúdo

Dicionário Python: Guia de Referência Rápida

[

Python Dictionary Cheat Sheet

Introduction

Welcome to the Python Dictionary Cheat Sheet! This guide will provide you with a comprehensive overview of dictionaries in Python, along with detailed, step-by-step examples, and explanations. Whether you’re new to Python or a seasoned developer, this cheat sheet will help you understand and utilize dictionaries effectively.

What is a Dictionary?

A dictionary in Python is an unordered collection of key-value pairs. It allows you to store, retrieve, and manipulate data using a unique key as an identifier. Dictionaries are mutable objects, meaning you can modify them after creation. They are denoted by curly braces {}, with each key-value pair separated by a colon :.

Creating a Dictionary

To create a dictionary in Python, you can use the curly braces {} and provide key-value pairs separated by a colon :. Here’s an example:

my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}

Accessing Values in a Dictionary

You can access the values in a dictionary by using the key associated with the value. Here are a few methods to retrieve values from a dictionary:

  • Using square brackets [] and the key:
name = my_dict['name']
  • Using the get() method:
age = my_dict.get('age')
  • Using the keys() and values() methods to access all keys and values:
keys = my_dict.keys()
values = my_dict.values()

Modifying Dictionary Values

Dictionaries allow you to modify the values associated with a particular key. Here’s how you can do it:

my_dict['name'] = 'Jane'

Adding and Removing Key-Value Pairs

To add a new key-value pair to a dictionary, you can assign a value to a new key:

my_dict['city'] = 'San Francisco'

To remove a key-value pair from a dictionary, you can use the del keyword:

del my_dict['age']

Dictionary Methods

Python provides various built-in methods for dictionaries. Here are a few commonly used ones:

MethodDescription
clear()Removes all key-value pairs from the dictionary
copy()Returns a shallow copy of the dictionary
update()Updates the dictionary with the key-value pairs from another dictionary
pop()Removes and returns the value for a specified key
items()Returns a view object that contains the key-value pairs

Dictionary Comprehension

Python also supports dictionary comprehension, which allows you to create dictionaries in a concise manner. Here’s an example:

my_dict = {x: x**2 for x in range(5)}

Conclusion

In this Python Dictionary Cheat Sheet, we covered the basics of dictionaries, including creating dictionaries, accessing values, modifying values, adding and removing key-value pairs, and using built-in methods. We also explored dictionary comprehension as a powerful way to create dictionaries. With this knowledge, you’ll be able to effectively utilize dictionaries in your Python projects.

Remember, dictionaries are versatile data structures that provide efficient key-value mapping, making them an essential tool in your Python programming journey!

Happy coding with Python!