nathanwailes

Dicts

Jun 9th, 2024
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. """
  2. """
  3. # Creating a dictionary
  4. my_dict = {
  5.     'name': 'Alice',
  6.     'age': 25,
  7.     'city': 'New York'
  8. }
  9.  
  10. # Accessing elements
  11. name = my_dict['name']              # Access the value associated with the key 'name'
  12.  
  13. # Modifying elements
  14. my_dict['age'] = 26                 # Change the value associated with the key 'age'
  15.  
  16. # Adding elements
  17. my_dict['email'] = '[email protected]'  # Add a new key-value pair
  18.  
  19. # Removing elements
  20. del my_dict['city']                 # Remove the key-value pair with the key 'city'
  21. removed_value = my_dict.pop('email') # Remove and return the value associated with 'email'
  22. last_item = my_dict.popitem()       # Remove and return the last inserted key-value pair (Python 3.7+)
  23.  
  24. # Checking for keys
  25. has_name = 'name' in my_dict        # Check if the key 'name' is in the dictionary
  26. has_city = 'city' in my_dict        # Check if the key 'city' is in the dictionary
  27.  
  28. # Getting the value of a key with a default
  29. age = my_dict.get('age', 0)         # Get the value for 'age', return 0 if 'age' is not found
  30. country = my_dict.get('country', 'Unknown')  # Get the value for 'country', return 'Unknown' if not found
  31.  
  32. # Iterating through a dictionary
  33. for key in my_dict:
  34.     print(key, my_dict[key])        # Print each key-value pair
  35.  
  36. for key, value in my_dict.items():
  37.     print(key, value)               # Print each key-value pair using items()
  38.  
  39. # Iterating through keys and values separately
  40. for key in my_dict.keys():
  41.     print(key)                      # Print each key
  42.  
  43. for value in my_dict.values():
  44.     print(value)                    # Print each value
  45.  
  46. # Dictionary comprehension
  47. squared_numbers = {x: x**2 for x in range(1, 6)}  # Create a dictionary with numbers and their squares
  48.  
  49. # Merging dictionaries
  50. another_dict = {'country': 'USA', 'gender': 'Female'}
  51. my_dict.update(another_dict)        # Merge another_dict into my_dict
  52.  
  53. # Clearing the dictionary
  54. my_dict.clear()                     # Remove all key-value pairs from the dictionary
  55.  
  56. # Dictionary methods
  57. keys = my_dict.keys()               # Get a view of all keys
  58. values = my_dict.values()           # Get a view of all values
  59. items = my_dict.items()             # Get a view of all key-value pairs
  60.  
  61. # Copying a dictionary
  62. copied_dict = my_dict.copy()        # Create a shallow copy of the dictionary
  63.  
  64. # Nested dictionaries
  65. nested_dict = {
  66.     'person1': {'name': 'Alice', 'age': 25},
  67.     'person2': {'name': 'Bob', 'age': 30}
  68. }
  69.  
  70. person1_name = nested_dict['person1']['name']  # Accessing nested dictionary elements
Advertisement
Add Comment
Please, Sign In to add comment