Exercise: Python

Questions for: Lists

How can you create a list of squared values from another list in Python?
A:
squared_list = list.square_values()
B:
squared_list = [x^2 for x in original_list]
C:
squared_list = list.map(lambda x: x**2, original_list)
D:
squared_list = [x**2 for x in original_list]
Answer: D
Using list comprehension, you can create a new list of squared values from an existing list.
What does the list.pop() method do without providing an index?
A:
Removes the last element from the list.
B:
Removes the first element from the list.
C:
Raises an error as an index is required.
D:
Removes all occurrences of a specific element.
Answer: A
Without providing an index, pop() removes and returns the last element from the list.
What is the purpose of the list.copy() method in Python?
A:
Creates a deep copy of the list.
B:
Creates a shallow copy of the list.
C:
Copies elements from one list to another.
D:
Concatenates two lists.
Answer: B
The copy() method in Python lists is used to create a shallow copy of the list.
How can you check if a specific element exists in a Python list?
A:
element in list
B:
list.contains(element)
C:
list.find(element)
D:
list.has(element)
Answer: A
The in keyword is used to check if a specific element exists in a Python list.
What is the purpose of the list.extend() method in Python?
A:
Adds a specified element to the list.
B:
Extends the list by appending elements from an iterable.
C:
Increases the length of the list.
D:
Concatenates two lists.
Answer: B
The extend() method in Python lists is used to extend the list by appending elements from an iterable.
Ad Slot (Above Pagination)
Quiz