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.

ZoeG2·48 flashcards·48 questions
collegecomputer_scienceprogramming
0
Known
1 / 48
0
Learning
Front

What is a Python list?

Tap to flip
Back

A Python list is an ordered collection of items that can be of different types, defined using square brackets, e.g., [1, 2, 3].

Tap to flip
Got it
Still learning

Quiz(48 questions)

Question 1 of 48

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?

A.Ordered collection of items
B.Unordered collection of key-value pairs
C.Ordered collection of key-value pairs
D.Unordered collection of items

2. What does it mean when we say a dictionary is mutable?

A.You can change its contents after creation.
B.It cannot hold more than one data type.
C.Its keys must be unique.
D.It is automatically sorted by keys.

3. What is a characteristic of a Python list?

A.It is ordered and mutable.
B.It is unordered and immutable.
C.It can only contain integers.
D.It is case-sensitive.

4. Which of the following is true about dictionary keys?

A.They can be duplicated
B.They must be unique
C.They are mutable
D.They can be any data type

5. Which of the following is the correct way to create a dictionary with initial values?

A.my_dict = {'a': 1, 'b': 2}
B.my_dict = ()
C.my_dict = []
D.my_dict = dict(a=1, b=2)

6. How do you retrieve the last item in a list?

A.list_name[-1]
B.list_name[len(list_name)]
C.list_name[last]
D.list_name[0]

7. How do you access the first element in a list named 'my_list'?

A.my_list[1]
B.my_list.first()
C.my_list[0]
D.my_list.get(0)

8. What will be the output if you attempt to access a non-existent key in a dictionary using square brackets?

A.It raises a KeyError.
B.It returns None.
C.It generates a default value.
D.It returns 'Key not found'.

9. True or False: You cannot change the items in a Python list after its creation.

A.True
B.False
C.Depends on the version of Python
D.Only if they are strings.

10. What will you get if you try to access a non-existent key in a dictionary?

A.None
B.KeyError
C.IndexError
D.ValueError

11. True or False: You can use a list as a dictionary key.

A.True
B.False
C.Only if the list is empty.
D.Only in Python 2.x.

12. Which method would you use to add multiple items to a list at once?

A.append()
B.extend()
C.insert()
D.add()

13. True or False: Lists can contain elements of different data types.

A.True
B.False
C.Only integers
D.Only strings

14. What does the pop() method do in a dictionary?

A.Removes and returns the value for a specified key.
B.Returns a view of all keys.
C.Adds a new key-value pair.
D.Clears all key-value pairs.

15. What will be the output of the expression: my_list = [5, 10, 15]; my_list = my_list + [20]?

A.[5, 10, 15]
B.[5, 10, 15, 20]
C.[20]
D.[5, 10, 15, 20, 20]

16. Which of the following methods adds a new item to the end of a list?

A.append()
B.add()
C.push()
D.insert()

17. How can you check if the dictionary my_dict contains the key 'x'?

A.'x' in my_dict
B.my_dict.contains('x')
C.my_dict['x']
D.check('x', my_dict)

18. What does the method pop() do when called on a list?

A.Adds an item to the list.
B.Removes an item by value.
C.Removes the last item and returns it.
D.Clears the entire list.

19. What data structure uses curly braces in Python?

A.List
B.Dictionary
C.Tuple
D.Set

20. What is the result of my_dict.items()?

A.A list of tuples containing all key-value pairs.
B.A single dictionary.
C.The first key-value pair as a tuple.
D.An empty list.

21. Which of the following statements is NOT true about list slicing?

A.It can return a new list.
B.It does not modify the original list.
C.It requires that the start index is less than the end index.
D.It can use negative indices.

22. Which operation will raise an IndexError?

A.Accessing a non-existent dictionary key
B.Accessing the 10th element of a list with 5 elements
C.Removing an existing key in a dictionary
D.Adding a new key-value pair

23. Which statement correctly removes the key 'item' from my_dict?

A.del my_dict['item']
B.my_dict.remove('item')
C.my_dict.pop('item')
D.my_dict['item'] = None

24. How would you confirm if a certain value exists in a list?

A.list_name.contains(value)
B.value in list_name
C.list_name.include(value)
D.list_name.has(value)

25. How can you remove an item from a list named 'my_list'?

A.pop()
B.remove()
C.del
D.clear()

26. What happens when you merge two dictionaries using the update() method?

A.Values from the second dictionary overwrite those from the first if keys match.
B.It creates a new dictionary.
C.It raises an error if keys overlap.
D.Only non-overlapping keys are added.

27. Which method would you use to sort a list in place?

A.sorted()
B.sort()
C.order()
D.arrange()

28. Which statement is true about the order of elements in lists and dictionaries?

A.Lists maintain order, dictionaries do not
B.Both maintain order
C.Neither maintain order
D.Dictionaries maintain order, lists do not

29. Which of the following is true about keys in a Python dictionary?

A.Keys must be unique within the dictionary.
B.Keys can be mutable.
C.Keys must be integers only.
D.Keys are automatically sorted.

30. How can you create a new list from an existing list using list comprehension?

A.Use the map() function.
B.List comprehension is not possible.
C.Use a for loop.
D.Use [x for x in list_name].

31. What is the result of using the del statement on a dictionary?

A.It removes the entire dictionary
B.It deletes a specific key-value pair
C.It clears all values
D.It cannot be used with dictionaries

32. How do you retrieve all the keys from my_dict?

A.my_dict.keys()
B.my_dict.get_keys()
C.my_dict.all_keys()
D.keys(my_dict)

33. What will my_list = [1, 2, 3] * 3 produce?

A.[1, 2, 3, 1, 2, 3, 1, 2, 3]
B.[1, 2, 3]
C.[3, 2, 1]
D.[1, 2, 3, 1]

34. What will be the output of my_list = [1, 2, 3] followed by my_list.append(4)?

A.[1, 2, 3]
B.[1, 2, 3, 4]
C.[4, 3, 2, 1]
D.Error

35. Which of the following statements is incorrect regarding Python dictionaries?

A.Dictionaries can store different data types.
B.Dictionaries maintain the order of their items.
C.Keys can be of any data type including lists.
D.Dictionaries are accessed using keys.

36. What will happen if you try to access an index that is out of range in a list?

A.It returns None.
B.It raises an IndexError.
C.It returns an empty list.
D.It loops back to the start.

37. Which of the following is NOT a valid way to initialize a dictionary?

A.my_dict = {}
B.my_dict = dict()
C.my_dict = []
D.my_dict = {'key': 'value'}

38. What will the expression len(my_dict) return?

A.The number of key-value pairs in the dictionary.
B.The number of keys only.
C.The size of the dictionary in bytes.
D.The number of values in the dictionary.

39. How do you remove the first occurrence of a specific value from a list?

A.delete(value)
B.remove(value)
C.discard(value)
D.erase(value)

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

A.Using contains()
B.Using in keyword
C.Using exists()
D.Using has_key()

41. What type of data structure is a nested dictionary?

A.A dictionary containing other dictionaries as values.
B.A dictionary with only integer keys.
C.A dictionary that is immutable.
D.A list of dictionaries.

42. True or False: A list can only contain elements of the same data type.

A.True
B.False
C.Only in Python 3
D.Only for performance reasons.

43. Which of the following statements is true about lists in Python?

A.Lists can have duplicate elements.
B.Lists use keys to access elements.
C.Lists are unordered collections.
D.Lists must contain the same data type.

44. How can you iterate through each key-value pair in my_dict?

A.for key, value in my_dict.items():
B.for (key, value) in my_dict:
C.my_dict.each()
D.for key in my_dict:

45. What is the result of calling the reverse() method on a list?

A.It returns a reversed copy of the list.
B.It sorts the list in descending order.
C.It modifies the list to be in reverse order.
D.It raises an error.

46. In Python, which of the following operations will result in a KeyError?

A.Accessing a non-existent key in a dictionary.
B.Removing an existing key from a dictionary.
C.Adding a new key-value pair to a dictionary.
D.Retrieving a value using an existing key.

47. Which of the following methods would you use to retrieve a value from a dictionary safely, without risking a KeyError?

A.get()
B.find()
C.retrieve()
D.fetch()

48. What will happen if you call the index() method on a value that is not in the list?

A.It will raise a ValueError.
B.It will return -1.
C.It will return None.
D.It will return the last index of the list.

Related Study Sets

Create Your Own Study Set

Upload a PDF, paste your notes, or describe a topic – AI generates flashcards, quizzes and more in seconds.