Empty Dictionary
martin-terragno / Cheatsheet
Cheatsheet
A concise cheat sheet covering Python dictionaries and tuples, including creation, manipulation, and common operations. Ideal for quick reference and efficient coding.
Dictionaries: Creation and Access
Dictionary Creation
|
|
Dictionary with Initial Values |
|
Dictionary using |
|
From a list of tuples |
|
Accessing Values
Using square brackets |
|
Using |
|
Handling missing keys |
|
Modifying Dictionaries
Adding new key-value pairs |
|
Updating existing values |
|
Deleting key-value pairs |
|
Using |
|
Using |
|
Dictionary Operations
Common Dictionary Methods
|
Returns a view object that displays a list of all the keys in the dictionary.
|
|
Returns a view object that displays a list of all the values in the dictionary.
|
|
Returns a view object that displays a list of a dictionary’s key-value tuple pairs.
|
|
Removes all items from the dictionary.
|
|
Returns a shallow copy of the dictionary.
|
|
Updates the dictionary with the elements from another dictionary object or from an iterable of key/value pairs.
|
Dictionary Comprehension
Basic Syntax |
|
Example: Creating a dictionary of squares |
|
Conditional Dictionary Comprehension |
|
Example: Filtering even numbers |
|
Membership Test
Using |
Checks if a key is present in the dictionary.
|
Using |
Checks if a key is not present in the dictionary.
|
Tuples: Creation and Basic Operations
Tuple Creation
Empty Tuple |
|
Tuple with Initial Values |
|
Tuple with a single element |
|
Using |
|
Accessing Tuple Elements
Using indexing |
|
Negative indexing |
|
Slicing |
|
Tuple Operations
Concatenation |
|
Repetition |
|
Length |
|
Membership Test |
|
Tuple Use Cases and Methods
Tuple Unpacking
Basic Unpacking |
|
Unpacking with |
|
Ignoring values during unpacking |
|
Tuple Methods
|
Returns the number of times a specified value occurs in a tuple.
|
|
Returns the index of the first occurrence of a value.
|
When to Use Tuples
|