Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Dictionary Methods in Python

Dictionary Methods in Python

? Dictionary Methods in Python

Python dictionaries come with several built-in methods that allow you to perform various operations like adding, removing, or modifying key-value pairs. Here's a list of commonly used dictionary methods:


? 1. clear()

The clear() method removes all items from the dictionary.

Example:

my_dict = {"name": "John", "age": 30, "city": "New York"}my_dict.clear()print(my_dict)  # Output: {}

? 2. copy()

The copy() method creates a shallow copy of the dictionary.

Example:

original_dict = {"name": "John", "age": 30}new_dict = original_dict.copy()print(new_dict)  # Output: {'name': 'John', 'age': 30}

? 3. fromkeys()

The fromkeys() method creates a new dictionary with specified keys and a default value.

Example:

keys = ["name", "age", "city"]default_value = "Unknown"my_dict = dict.fromkeys(keys, default_value)print(my_dict)  # Output: {'name': 'Unknown', 'age': 'Unknown', 'city': 'Unknown'}

? 4. get()

The get() method returns the value of the specified key. If the key doesn't exist, it returns None or a default value if provided.

Example:

my_dict = {"name": "John", "age": 30}print(my_dict.get("name"))  # Output: Johnprint(my_dict.get("city", "Not Found"))  # Output: Not Found

? 5. items()

The items() method returns a view object that displays a list of a dictionary's key-value tuple pairs.

Example:

my_dict = {"name": "John", "age": 30}for key, value in my_dict.items():    print(key, value)

? Output:

name Johnage 30

? 6. keys()

The keys() method returns a view object of all the dictionary's keys.

Example:

my_dict = {"name": "John", "age": 30}print(my_dict.keys())  # Output: dict_keys(['name', 'age'])

? 7. pop()

The pop() method removes and returns the value associated with the specified key.

Example:

my_dict = {"name": "John", "age": 30}removed_value = my_dict.pop("age")print(removed_value)  # Output: 30print(my_dict)  # Output: {'name': 'John'}

? 8. popitem()

The popitem() method removes and returns the last key-value pair as a tuple. In Python 3.7+, dictionaries are ordered, so the last inserted item is removed.

Example:

my_dict = {"name": "John", "age": 30, "city": "New York"}last_item = my_dict.popitem()print(last_item)  # Output: ('city', 'New York')print(my_dict)  # Output: {'name': 'John', 'age': 30}

? 9. setdefault()

The setdefault() method returns the value of the key if it exists; otherwise, it inserts the key with the provided default value.

Example:

my_dict = {"name": "John", "age": 30}print(my_dict.setdefault("city", "New York"))  # Output: New Yorkprint(my_dict)  # Output: {'name': 'John', 'age': 30, 'city': 'New York'}

? 10. update()

The update() method updates the dictionary with the key-value pairs from another dictionary or iterable.

Example:

my_dict = {"name": "John", "age": 30}new_data = {"age": 31, "city": "New York"}my_dict.update(new_data)print(my_dict)  # Output: {'name': 'John', 'age': 31, 'city': 'New York'}

? 11. values()

The values() method returns a view object of all the dictionary's values.

Example:

my_dict = {"name": "John", "age": 30}print(my_dict.values())  # Output: dict_values(['John', 30])

? 12. __contains__()

The __contains__() method checks if a key is in the dictionary. It is used by the in operator.

Example:

my_dict = {"name": "John", "age": 30}print("name" in my_dict)  # Output: Trueprint("city" in my_dict)  # Output: False

? 13. pop() with Default Value

You can also specify a default value when using the pop() method. If the key does not exist, the method returns the default value.

Example:

my_dict = {"name": "John", "age": 30}removed_value = my_dict.pop("city", "Not Found")print(removed_value)  # Output: Not Found

? Summary of Common Dictionary Methods

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

  • copy(): Creates a shallow copy of the dictionary.

  • fromkeys(): Creates a dictionary with specified keys and a default value.

  • get(): Returns the value for a specified key (or default if not found).

  • items(): Returns a view object containing key-value pairs.

  • keys(): Returns a view object containing all dictionary keys.

  • pop(): Removes and returns the value for the specified key.

  • popitem(): Removes and returns the last key-value pair as a tuple.

  • setdefault(): Returns the value for a key, or inserts it with a default value.

  • update(): Updates the dictionary with another dictionary or iterable.

  • values(): Returns a view object containing all dictionary values.

Let me know if you need further clarification or examples! ?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql