Intro to Programming · Intro Programming Topics37 flashcards

Intro Programming Dictionaries and Hash Maps

37 flashcards covering Intro Programming Dictionaries and Hash Maps for the INTRO-PROGRAMMING Intro Programming Topics section.

Dictionaries and hash maps are essential data structures in programming that store key-value pairs, allowing for efficient data retrieval and manipulation. These concepts are defined in many introductory programming curricula, including those from organizations like the Association for Computing Machinery (ACM). Understanding how to utilize these structures is fundamental for managing collections of data, especially when performance and quick access are priorities.

On practice exams and competency assessments, questions about dictionaries and hash maps often require you to identify their properties, compare them to other data structures, or solve coding problems that involve their use. Common traps include confusing the terms and functionalities of dictionaries with lists or arrays, and misapplying methods for adding or retrieving data. It's crucial to pay attention to the syntax and the specific operations that can be performed on these structures to avoid errors.

A practical tip to keep in mind is to always consider the efficiency of your data retrieval needs—choose the right structure based on whether you need fast lookups or ordered data.

Terms (37)

  1. 01

    What is a dictionary in Python?

    A dictionary in Python is a collection of key-value pairs, where each key is unique and is used to access the corresponding value. It is defined using curly braces, with keys and values separated by colons (Think Python, Chapter 7).

  2. 02

    How do you create an empty dictionary in Python?

    An empty dictionary can be created using empty curly braces {} or the dict() function. Both methods will result in a dictionary with no items (Think Python, Chapter 7).

  3. 03

    What method is used to retrieve a value from a dictionary?

    The method used to retrieve a value from a dictionary is by using the key in square brackets, e.g., dict[key], or by using the get() method, e.g., dict.get(key) (Think Python, Chapter 7).

  4. 04

    What happens if you access a non-existent key in a dictionary?

    If you access a non-existent key using square brackets, a KeyError will be raised. However, using the get() method will return None or a specified default value instead (Think Python, Chapter 7).

  5. 05

    How do you add a new key-value pair to a dictionary?

    To add a new key-value pair to a dictionary, you assign a value to a new key using the syntax dict[key] = value (Think Python, Chapter 7).

  6. 06

    What is the difference between a list and a dictionary in Python?

    The primary difference is that lists are ordered collections of items accessed by their index, while dictionaries are unordered collections of key-value pairs accessed by keys (Harvard CS50, Week 2).

  7. 07

    How can you remove a key-value pair from a dictionary?

    You can remove a key-value pair from a dictionary using the del statement, e.g., del dict[key], or by using the pop() method, which also returns the value (Think Python, Chapter 7).

  8. 08

    What method would you use to get all the keys from a dictionary?

    To get all the keys from a dictionary, you can use the keys() method, which returns a view object displaying a list of all the keys (Think Python, Chapter 7).

  9. 09

    How can you check if a key exists in a dictionary?

    You can check if a key exists in a dictionary using the 'in' keyword, e.g., key in dict, which returns True if the key is present and False otherwise (Think Python, Chapter 7).

  10. 10

    What is a hash map?

    A hash map is a data structure that implements an associative array, a structure that can map keys to values. It uses a hash function to compute an index into an array of buckets or slots (Think Python, Chapter 7).

  11. 11

    How does a hash function work in a hash map?

    A hash function takes an input (key) and returns an integer, which determines the index in the hash map where the corresponding value is stored (Think Python, Chapter 7).

  12. 12

    What is a collision in a hash map?

    A collision occurs when two different keys hash to the same index in a hash map, which can lead to multiple values being stored at that index (Think Python, Chapter 7).

  13. 13

    What is one common method for resolving collisions in hash maps?

    One common method for resolving collisions is chaining, where each index in the hash map points to a linked list of entries that hash to the same index (Think Python, Chapter 7).

  14. 14

    What is the time complexity for searching in a hash map?

    The average time complexity for searching in a hash map is O(1), meaning it can be done in constant time. However, in the worst case, it can be O(n) due to collisions (Think Python, Chapter 7).

  15. 15

    How can you iterate over a dictionary in Python?

    You can iterate over a dictionary using a for loop, which by default iterates over the keys. You can also use items() to iterate over key-value pairs (Think Python, Chapter 7).

  16. 16

    What is the purpose of the update() method in a dictionary?

    The update() method is used to add key-value pairs from another dictionary or from an iterable of key-value pairs to an existing dictionary (Think Python, Chapter 7).

  17. 17

    What is the difference between the pop() and popitem() methods in a dictionary?

    The pop() method removes a specified key and returns its value, while popitem() removes and returns the last inserted key-value pair as a tuple (Think Python, Chapter 7).

  18. 18

    How do you merge two dictionaries in Python?

    You can merge two dictionaries using the update() method or the merge operator (|) in Python 3.9 and later (Think Python, Chapter 7).

  19. 19

    What is the syntax to create a dictionary with initial values?

    You can create a dictionary with initial values using curly braces with key-value pairs, e.g., dict = {'key1': 'value1', 'key2': 'value2'} (Think Python, Chapter 7).

  20. 20

    How can you create a dictionary from two lists in Python?

    You can create a dictionary from two lists using the zip() function combined with the dict() constructor, e.g., dict(zip(keys, values)) (Think Python, Chapter 7).

  21. 21

    What is the method to clear all items from a dictionary?

    The clear() method is used to remove all items from a dictionary, leaving it empty (Think Python, Chapter 7).

  22. 22

    How do you find the length of a dictionary?

    You can find the length of a dictionary using the len() function, which returns the number of key-value pairs in the dictionary (Think Python, Chapter 7).

  23. 23

    What is a nested dictionary?

    A nested dictionary is a dictionary that contains other dictionaries as values, allowing for more complex data structures (Think Python, Chapter 7).

  24. 24

    How can you access a value in a nested dictionary?

    To access a value in a nested dictionary, you use multiple keys in succession, e.g., dict[key1][key2] (Think Python, Chapter 7).

  25. 25

    What is the difference between shallow copy and deep copy in dictionaries?

    A shallow copy creates a new dictionary but does not create copies of nested objects, while a deep copy creates a new dictionary along with copies of all nested objects (Think Python, Chapter 7).

  26. 26

    How can you convert a dictionary to a list of tuples?

    You can convert a dictionary to a list of tuples using the items() method, which returns a view of the dictionary's key-value pairs (Think Python, Chapter 7).

  27. 27

    What is the output of dict.get(key, default) if the key does not exist?

    If the key does not exist, dict.get(key, default) returns the specified default value instead of raising an error (Think Python, Chapter 7).

  28. 28

    How do you sort a dictionary by its keys?

    You can sort a dictionary by its keys using the sorted() function on the dictionary's keys, e.g., sorted(dict.keys()) (Think Python, Chapter 7).

  29. 29

    What is the method to get all the values from a dictionary?

    To get all the values from a dictionary, you can use the values() method, which returns a view object displaying a list of all the values (Think Python, Chapter 7).

  30. 30

    How can you check the number of occurrences of a key in a dictionary?

    In a dictionary, keys are unique, so a key can either exist once or not at all. You can check for existence using 'in' but not count occurrences (Think Python, Chapter 7).

  31. 31

    What is the purpose of the fromkeys() method in a dictionary?

    The fromkeys() method creates a new dictionary with specified keys and assigns them a specified value (Think Python, Chapter 7).

  32. 32

    How do you create a dictionary comprehension?

    A dictionary comprehension is created using curly braces with an expression, e.g., {key: value for item in iterable} (Think Python, Chapter 7).

  33. 33

    What is the difference between mutable and immutable types in Python in relation to dictionaries?

    Dictionaries are mutable, meaning they can be changed after creation, whereas immutable types like strings and tuples cannot be modified (Think Python, Chapter 7).

  34. 34

    What is the significance of the hash value in a hash map?

    The hash value determines the index where the key-value pair is stored in a hash map, affecting performance and collision resolution (Think Python, Chapter 7).

  35. 35

    How can you use a dictionary to count occurrences of items in a list?

    You can iterate through the list and use a dictionary to map each item to its count by incrementing the count for each occurrence (Think Python, Chapter 7).

  36. 36

    What is the role of keys in a dictionary?

    Keys in a dictionary serve as unique identifiers for accessing their corresponding values, ensuring that each key maps to only one value (Think Python, Chapter 7).

  37. 37

    How do you handle exceptions when accessing dictionary keys?

    You can handle exceptions by using try-except blocks around the key access or by using the get() method to avoid KeyErrors (Think Python, Chapter 7).