Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python dict Built-in methods

Python dictionaries have several built-in methods that allow you to perform various operations on them. Here is a list of commonly used dictionary methods:

  1. clear(): Removes all items from the dictionary.

    my_dict = {'a': 1, 'b': 2, 'c': 3}
    my_dict.clear()
    print(my_dict)  # Output: {}
    
  2. copy(): Returns a shallow copy of the dictionary.

    original = {'a': 1, 'b': 2, 'c': 3}
    copied = original.copy()
    print(copied)  # Output: {'a': 1, 'b': 2, 'c': 3}
    
  3. get(key, default=None): Returns the value for the specified key if it exists, otherwise returns the default value.

    my_dict = {'a': 1, 'b': 2, 'c': 3}
    value = my_dict.get('d', 'default')
    print(value)  # Output: 'default'
    
  4. items(): Returns a view object displaying a list of the dictionary's key-value pairs as tuples.

    my_dict = {'a': 1, 'b': 2, 'c': 3}
    for key, value in my_dict.items():
        print(key, value)
    
  5. keys(): Returns a view object displaying a list of the dictionary's keys.

    my_dict = {'a': 1, 'b': 2, 'c': 3}
    for key in my_dict.keys():
        print(key)
    
  6. values(): Returns a view object displaying a list of the dictionary's values.

    my_dict = {'a': 1, 'b': 2, 'c': 3}
    for value in my_dict.values():
        print(value)
    
  7. pop(key, default=None): Removes and returns the value for the specified key. If the key is not found and a default value is provided, it returns the default value; otherwise, it raises a KeyError.

    my_dict = {'a': 1, 'b': 2, 'c': 3}
    value = my_dict.pop('b')
    print(value)  # Output: 2
    print(my_dict)  # Output: {'a': 1, 'c': 3}
    
  8. popitem(): Removes and returns an arbitrary key-value pair as a tuple. If the dictionary is empty, it raises a KeyError.

    my_dict = {'a': 1, 'b': 2, 'c': 3}
    key, value = my_dict.popitem()
    print(key, value)
    
  9. setdefault(key, default=None): Returns the value for the specified key if it exists, otherwise sets the key to the specified default value and returns the default value.

    my_dict = {'a': 1, 'b': 2, 'c': 3}
    value = my_dict.setdefault('d', 4)
    print(value)  # Output: 4
    print(my_dict)  # Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    
  1. Common Dictionary Operations in Python:

    • Operations include creating, accessing, modifying, and deleting key-value pairs.
    # Example
    my_dict = {'a': 1, 'b': 2, 'c': 3}
    
  2. Python dict keys() Method:

    • The keys() method returns a view of all keys in the dictionary.
    # Example
    keys = my_dict.keys()
    
  3. values() and items() Methods in Python Dictionaries:

    • The values() method returns a view of all values, and items() returns key-value pairs.
    # Example
    values = my_dict.values()
    items = my_dict.items()
    
  4. How to Use get() Method with Dictionaries in Python:

    • The get() method retrieves the value for a given key, or a default value if the key is not present.
    # Example
    value = my_dict.get('a', 'Key not found')
    
  5. Updating and Modifying Dictionaries in Python:

    • Use assignment to update or add key-value pairs.
    # Example
    my_dict['d'] = 4  # Add a new key-value pair
    my_dict['a'] = 10  # Update an existing value
    
  6. Python Dictionary pop() and popitem() Methods:

    • The pop() method removes and returns the value for a given key.
    • The popitem() method removes and returns the last key-value pair.
    # Example
    popped_value = my_dict.pop('b')
    popped_item = my_dict.popitem()
    
  7. Dictionary Comprehension vs dict Methods in Python:

    • Dictionary comprehension is a concise way to create dictionaries.
    • Dict methods (keys(), values(), items()) provide dynamic views on dictionary contents.
    # Example
    squared_dict_comp = {key: value**2 for key, value in my_dict.items()}
    squared_dict_method = dict((key, value**2) for key, value in my_dict.items())