Explore key differences between Python lists and tuples, discover seven frequent mistakes developers encounter, and learn how to fix them for efficient backend development. This quiz covers list and tuple syntax, mutability, performance, and best practices for choosing between the two data structures.
Which of the following can you modify after creation: a Python list or a tuple?
Explanation: Lists in Python are mutable, which means their elements can be changed after creation. Tuples, on the other hand, are immutable and cannot be modified once created. The option 'Both a list and a tuple' is incorrect because only lists are mutable. 'Only a tuple' and 'Neither a list nor a tuple' disregard the mutability feature of lists.
To declare a tuple in Python, which type of brackets should you use: parentheses or square brackets?
Explanation: Tuples in Python are defined using parentheses, such as (1, 2, 3). Lists are declared with square brackets; curly brackets are used for sets or dictionaries, while angle brackets are not used in this context. Misusing square brackets or curly brackets will create a different data type.
What happens if you create a tuple like (5) instead of (5,)?
Explanation: (5) is treated as an integer with value 5, not as a tuple. Adding a comma, as in (5,), is the correct way to create a single-element tuple. The other options are incorrect: (5) does not create a tuple, list, or string in this scenario. Forgetting the comma is a common beginner mistake.
Which method is supported by lists but NOT by tuples in Python?
Explanation: The append() method is only available for lists, allowing new elements to be added. Both count() and index() exist for both lists and tuples, while len() is a general function used on both. Choosing count(), index(), or len() would be incorrect since these work with tuples as well.
When storing a large collection of constant values, which is generally more memory-efficient in Python: a list or a tuple?
Explanation: Tuples are typically more memory-efficient than lists because they are immutable and carry less overhead. Lists have extra features for item mutations, which makes them use more memory. 'Both' is incorrect since there is a difference, and 'A set' is unrelated to the comparison asked.
Why might you choose a tuple over a list for storing coordinates (like (x, y))?
Explanation: The immutability of tuples helps prevent accidental changes to data like coordinates. Tuples cannot be appended to, so fast appending is not an advantage. Tuples can store any datatype, not just strings, and they are usually faster, not slower, when it comes to iteration.
Can you use slicing (e.g., x[1:3]) with both Python lists and tuples?
Explanation: Slicing syntax is available for both lists and tuples, allowing you to access sub-parts of either data structure. Saying it's available only on lists or only on tuples would be incorrect, as Python supports this operation for any sequence type. Neither is also incorrect.
Which of the following is a valid nested structure: a list inside a tuple?
Explanation: Tuples can hold any type of object, including mutable ones like lists. The idea that tuples cannot contain mutable items is false; they simply cannot themselves be modified. Tuples and lists can be nested within each other, so the last two options are incorrect.
Which function can you use to find the position of an element in both a list and a tuple?
Explanation: The index() function locates the position of a specified value in both lists and tuples. update() does not exist for either, pop() removes items only in lists, and insert() adds elements to lists. Thus, only index() fits the scenario described.
Given the value y = [1, 2, 3], what is the type of y in Python?
Explanation: Square brackets in Python define lists, so y is a list. Tuples use parentheses, sets use curly braces, and dict is a mapping type. Mistaking these for each other can cause confusion in type-dependent operations.
Can you unpack values from both a list and a tuple in Python using assignment (e.g., a, b = [1, 2])?
Explanation: Python allows unpacking of values from both lists and tuples using assignment, so this feature is not limited to one or the other. Saying it's only for strings, or only for one type, is incorrect. Both structures support this convenient syntax.
What is the result of converting a tuple to a list using list((4, 5)) in Python?
Explanation: The list() function converts a tuple into a list, so (4, 5) becomes [4, 5]. Option '(4, 5)' keeps the tuple form, '{4, 5}' would be a set, and the string option is not a result of this operation. Selecting any but the first option is a common conversion mistake.
Why can tuples but not lists be used as keys in a Python dictionary?
Explanation: The immutability and hashability of tuples make them usable as dictionary keys. Lists, being mutable, cannot serve as keys. The size or memory usage of the containers is unrelated to this property, making the other choices invalid.
What is the result of (1, 2) + (3, 4) in Python?
Explanation: Using + on two tuples creates a new, combined tuple; so (1, 2) + (3, 4) yields (1, 2, 3, 4). The operation does not convert to a list or a set, and the last option is not valid syntax. Picking an incorrect structure or syntax is a frequent error.
Which built-in function can you use to determine the number of elements in both a list and a tuple?
Explanation: The len() function returns the number of elements in both lists and tuples. sum() gives the total of numerical values, size() and has() are not built-in functions in this context. Selecting the wrong function can cause errors or unexpected results.
What happens if you attempt to change the first element of a tuple, e.g., t = (7, 8); t[0] = 9?
Explanation: Attempting to modify a tuple results in a TypeError because tuples are immutable. The tuple cannot be changed as described, and Python does not create a list in this scenario. Doing nothing or silently changing the tuple is not the Python behavior.