Mapa mental
Dictionaries
Contenido del mapa
Dictionaries
Definition
- A data structure
- Stores key-value pairs
- Mutable and unordered
- A data structure
Characteristics
- Keys
- Unique within a dictionary
- Immutable types (e.g., strings, numbers, tuples)
- Values
- Can be of any data type
- Can be duplicated
- Keys
Creating a Dictionary
- Using curly braces
- Syntax: {key1: value1, key2: value2}
- Using the dict() constructor
- Syntax: dict(key1=value1, key2=value2)
- Using curly braces
Accessing Elements
- Using keys
- Syntax: dict_name[key]
- Using get() method
- Syntax: dict_name.get(key)
- Using keys
Modifying a Dictionary
- Adding new key-value pairs
- Syntax: dict_name[new_key] = new_value
- Updating existing key-value pairs
- Syntax: dict_name[existing_key] = new_value
- Adding new key-value pairs
Built-in functions
- len()
- Function that returns the number of items
- Counts key-value pairs
- Example: len(my_dict)
- dict()
- Create dictionary objects
- keys()
- Retrieves keys from a dictionary
- Syntax: my_dict.keys()
- values()
- Retrieves values from the dictionary
- Syntax: my_dict.values()
- items()
- Retrieves key-value pairs
- Syntax: dict.items()
- update()
- Method to update dictionaries
- Adds new key-value pairs
- Updates existing key values
- Syntax: dict1.update(dict2)
- Method to update dictionaries
- get()
- Method for retrieving values
- Syntax: dict.get(key, default)
- del
- Removes a specified item;
- Syntax: del dictionary[key];
- Removes a specified item;
- clear
- Removes all items
- Empties the dictionary
- Example: my_dict.clear()
- fromkeys()
- A method for creating a new dictionary.
- Examples
- Creating a dictionary with specified keys.
- Example: `dict.fromkeys(['a', 'b', 'c'])`
- Creating a dictionary with specified keys and a default value.
- Example: `dict.fromkeys(['x', 'y', 'z'], 0)`
- Creating a dictionary with specified keys.
- Examples
- A method for creating a new dictionary.
- copy()
- Creates a shallow copy of a dictionary
- dict.copy() method
- Creates a shallow copy of a dictionary
- pop()
- Remove an item
- Based on the provided key
- Syntax: dict.pop(key[, default])
- Based on the provided key
- Remove an item
- popitem()
- Method to remove and return an arbitrary (last) key-value pair
- setdefault()
- Retrieve value if key exists
- If key not present, add key with default value
- Syntax: dict.setdefault(key, default=None)
- If key not present, add key with default value
- Retrieve value if key exists
- max(), min()
- max()
- Returns the largest item;
- min()
- Returns the smallest item;
- max()
- sorted()
- Returns a sorted list of the dictionary keys
- Example: sorted(d)
- Returns a sorted list of the dictionary keys
- len()
Información
- Descripción
- Mapa Mental sobre Dictionaries, creado por Suja Nair el 17/05/2025.
- Creado por
- Suja Nair
- Visitas
- 1
- Valoración