CheatSheet: Dictionaries & Sets
| Package/Method | Description | Code Example |
|---|---|---|
| Creating a Dictionary | A dictionary is a built-in data type that represents a collection of key-value pairs. Dictionaries are enclosed in curly braces | Example:
|
| Accessing Values | You can access the values in a dictionary using their corresponding | Syntax:
Example:
|
Add or modify | Inserts a new key-value pair into the dictionary. If the key already exists, the value will be updated; otherwise, a new entry is created. | Syntax:
Example:
|
| del | Removes the specified key-value pair from the dictionary. Raises a | Syntax:
Example:
|
| update() | The | Syntax:
Example:
|
| clear() | The | Syntax:
Example:
|
| key existence | You can check for the existence of a key in a dictionary using the | Example:
|
| copy() | Creates a shallow copy of the dictionary. The new dictionary contains the same key-value pairs as the original, but they remain distinct objects in memory. | Syntax:
Example:
|
| keys() | Retrieves all keys from the dictionary and converts them into a list. Useful for iterating or processing keys using list methods. | Syntax:
Example:
|
| values() | Extracts all values from the dictionary and converts them into a list. This list can be used for further processing or analysis. | Syntax:
Example:
|
| items() | Retrieves all key-value pairs as tuples and converts them into a list of tuples. Each tuple consists of a key and its corresponding value. | Syntax:
Example:
|
Sets
| Package/Method | Description | Code Example |
|---|---|---|
| add() | Elements can be added to a set using the `add()` method. Duplicates are automatically removed, as sets only store unique values. | Syntax:
Example:
|
| clear() | The `clear()` method removes all elements from the set, resulting in an empty set. It updates the set in-place. | Syntax:
Example:
|
| copy() | The `copy()` method creates a shallow copy of the set. Any modifications to the copy won't affect the original set. | Syntax:
Example:
|
| Defining Sets | A set is an unordered collection of unique elements. Sets are enclosed in curly braces `{}`. They are useful for storing distinct values and performing set operations. | Example:
|
| discard() | Use the `discard()` method to remove a specific element from the set. Ignores if the element is not found. | Syntax:
Example:
|
| issubset() | The `issubset()` method checks if the current set is a subset of another set. It returns True if all elements of the current set are present in the other set, otherwise False. | Syntax:
Example:
|
| issuperset() | The `issuperset()` method checks if the current set is a superset of another set. It returns True if all elements of the other set are present in the current set, otherwise False. | Syntax: is_superset = set1.issuperset(set2) Example:
|
| pop() | The `pop()` method removes and returns an arbitrary element from the set. It raises a `KeyError` if the set is empty. Use this method to remove elements when the order doesn't matter. | Syntax:
Example:
|
| remove() | Use the `remove()` method to remove a specific element from the set. Raises a `KeyError` if the element is not found. | Syntax:
Example:
|
| Set Operations | Perform various operations on sets: `union`, `intersection`, `difference`, `symmetric difference`. | Syntax:
Example:
|
| update() | The `update()` method adds elements from another iterable into the set. It maintains the uniqueness of elements. | Syntax:
Example:
|
Comments
Post a Comment