Python lists and dictionaries exam review
Review essential concepts of Python lists and dictionaries through a series of questions and answers designed for college-level students.
Quiz(48 questions)
1. What type of collection is a list in Python?
Terms in this Study Set(48)
Python Lists(16)
What is a Python list?
A Python list is an ordered collection of items that can be of different types, defined using square brackets, e.g., [1, 2, 3].
How do you access the first element in a list?
Use the index 0: list_name[0]. Example: my_list = [10, 20, 30] → my_list[0] returns 10.
True or False: Lists are immutable.
False. Lists are mutable, meaning you can change their content after creation.
How to add an item to a list?
Use the append() method: list_name.append(item). This adds the item to the end of the list.
What function returns the length of a list?
The len() function. Example: len(my_list) returns the number of elements in my_list.
How do you remove an item by value?
Use the remove() method: list_name.remove(value). It deletes the first occurrence of value.
What does list slicing do?
List slicing extracts a portion of the list, e.g., my_list[1:3] gets elements at index 1 and 2.
Fill in the blank: To sort a list, use ___ method.
sort(). This arranges the list in ascending order.
How do you check if an item is in a list?
Use the 'in' keyword: if item in list_name checks for presence.
What is the difference between append() and extend()?
append() adds a single element, while extend() adds elements from another iterable to the list.
What will my_list = [1, 2, 3] * 2 result in?
The result will be [1, 2, 3, 1, 2, 3], as it repeats the list.
How do you reverse a list?
Use the reverse() method: list_name.reverse(). This modifies the list in place.
True or False: Lists can contain other lists.
True. Lists can contain any data type, including other lists, allowing for nested structures.
What is list comprehension?
A concise way to create lists. Example: [x*2 for x in range(5)] results in [0, 2, 4, 6, 8].
How do you find the index of an item?
Use the index() method: list_name.index(value) returns the index of the first occurrence of value.
Cause → Effect: If you use pop() on an empty list, what happens?
It raises an IndexError, indicating that there are no items to pop.
Python Dictionaries(16)
What is a Python dictionary?
A mutable, unordered collection of key-value pairs. Keys are unique.
How do you create an empty dictionary?
Use curly braces: my_dict = {} or my_dict = dict().
True or False: Dictionary keys can be mutable.
False - Keys must be immutable types like strings, numbers, or tuples.
Fill in the blank: A dictionary uses _____ to access values.
Keys
What method retrieves a value safely?
Use the get() method: my_dict.get(key, default) returns default if key not found.
How to add an item to a dictionary?
Assign value to a new key: my_dict[key] = value.
Name one way to remove an item from a dictionary.
Use del statement: del my_dict[key].
How do you check if a key exists?
Use the 'in' keyword: key in my_dict.
What does the items() method return?
A view object displaying a list of a dictionary's key-value tuple pairs.
What is a common use of dictionaries?
- Storing related data - Counting occurrences of items - Fast lookups
True or False: Dictionaries maintain the order of items.
True - As of Python 3.7, dictionaries maintain insertion order.
How do you merge two dictionaries?
Use the update() method: dict1.update(dict2).
What is the output of my_dict.get('key', 'default')?
Returns the value for 'key', or 'default' if 'key' is not found.
What is a nested dictionary?
A dictionary containing other dictionaries as values.
How can you iterate through a dictionary?
Use a for loop: for key in my_dict: print(key, my_dict[key]).
What function gives the number of items in a dictionary?
Use len(my_dict) to get the count of key-value pairs.
List and Dictionary Comparisons(16)
What is a list?
A list is an ordered collection of items. It allows duplicates and can contain mixed data types.
What is a dictionary?
A dictionary is an unordered collection of key-value pairs. Keys are unique and must be immutable.
Lists are indexed by...
Lists are indexed by integers, starting from 0. Example: my_list[0] accesses the first element.
Dictionaries are accessed by...
Dictionaries are accessed by keys, not by indices. Example: my_dict['key'] retrieves the associated value.
True or False: Lists use curly braces.
False. Lists use square brackets, while dictionaries use curly braces.
Fill in the blank: A dictionary's keys must be _____
unique and immutable (e.g., strings, numbers, tuples).
Similarities between lists and dictionaries?
- Both can store multiple items. - Both are mutable. - Both can contain mixed data types.
How to add an item to a list?
Use append(): my_list.append('new_item'). This adds 'new_item' to the end of the list.
How to add a key-value pair to a dictionary?
Assign a value to a new key: my_dict['new_key'] = 'new_value'.
True or False: Lists can have non-unique elements.
True. Lists can contain the same element multiple times.
What happens when you access a non-existent key?
Accessing a non-existent key in a dictionary raises a KeyError. Lists return an IndexError if indices are out of range.
Cause → Effect: Adding elements to a list.
Cause: Using append(). Effect: List grows in size.
How can you remove an item from a list?
Use remove(): my_list.remove('item_to_remove'). This deletes the first occurrence of the item.
How can you remove a key from a dictionary?
Use del: del my_dict['key_to_remove']. This deletes the specified key-value pair.
What is the primary difference in order?
Lists maintain order (elements are in sequence), while dictionaries do not guarantee order (until Python 3.7, where insertion order is preserved).
Example of a list and a dictionary:
List: fruits = ['apple', 'banana', 'cherry'] Dictionary: person = {'name': 'Alice', 'age': 30}
Questions in this Study Set(48)
1. What type of collection is a list in Python?
2. What does it mean when we say a dictionary is mutable?
3. What is a characteristic of a Python list?
4. Which of the following is true about dictionary keys?
5. Which of the following is the correct way to create a dictionary with initial values?
6. How do you retrieve the last item in a list?
7. How do you access the first element in a list named 'my_list'?
8. What will be the output if you attempt to access a non-existent key in a dictionary using square brackets?
9. True or False: You cannot change the items in a Python list after its creation.
10. What will you get if you try to access a non-existent key in a dictionary?
11. True or False: You can use a list as a dictionary key.
12. Which method would you use to add multiple items to a list at once?
13. True or False: Lists can contain elements of different data types.
14. What does the pop() method do in a dictionary?
15. What will be the output of the expression: my_list = [5, 10, 15]; my_list = my_list + [20]?
16. Which of the following methods adds a new item to the end of a list?
17. How can you check if the dictionary my_dict contains the key 'x'?
18. What does the method pop() do when called on a list?
19. What data structure uses curly braces in Python?
20. What is the result of my_dict.items()?
21. Which of the following statements is NOT true about list slicing?
22. Which operation will raise an IndexError?
23. Which statement correctly removes the key 'item' from my_dict?
24. How would you confirm if a certain value exists in a list?
25. How can you remove an item from a list named 'my_list'?
26. What happens when you merge two dictionaries using the update() method?
27. Which method would you use to sort a list in place?
28. Which statement is true about the order of elements in lists and dictionaries?
29. Which of the following is true about keys in a Python dictionary?
30. How can you create a new list from an existing list using list comprehension?
31. What is the result of using the del statement on a dictionary?
32. How do you retrieve all the keys from my_dict?
33. What will my_list = [1, 2, 3] * 3 produce?
34. What will be the output of my_list = [1, 2, 3] followed by my_list.append(4)?
35. Which of the following statements is incorrect regarding Python dictionaries?
36. What will happen if you try to access an index that is out of range in a list?
37. Which of the following is NOT a valid way to initialize a dictionary?
38. What will the expression len(my_dict) return?
39. How do you remove the first occurrence of a specific value from a list?
40. How can you check if a key exists in a dictionary?
41. What type of data structure is a nested dictionary?
42. True or False: A list can only contain elements of the same data type.
43. Which of the following statements is true about lists in Python?
44. How can you iterate through each key-value pair in my_dict?
45. What is the result of calling the reverse() method on a list?
46. In Python, which of the following operations will result in a KeyError?
47. Which of the following methods would you use to retrieve a value from a dictionary safely, without risking a KeyError?
48. What will happen if you call the index() method on a value that is not in the list?
Related Study Sets
Abitur Rekursion
Abitur: Abitur Klassen und Objekte
Was ist ein Algorithmus Schritt für Schritt
if und Schleifen Notizen
Wiederholung: Funktionen
Test: Binärzahlen
Listen Notizen
Schleife Alltag Beispiel Begriffe
Create Your Own Study Set
Upload a PDF, paste your notes, or describe a topic – AI generates flashcards, quizzes and more in seconds.

