Worksheet on python Lists
- How can you create an empty list in Python?
- A)
list = [] - B)
list = {} - C)
list = () - D)
list = //
- A)
- Which function is used to add an element at the end of the list?
- A)
push() - B)
append() - C)
insert() - D)
extend()
- A)
- What does the
len()function return when used with a list?- A) The first element of the list
- B) The last element of the list
- C) The number of elements in the list
- D) A boolean value indicating if the list is empty
- Which method inserts an item at a specified index in a list?
- A)
append() - B)
extend() - C)
insert() - D)
add()
- A)
- How can you remove the last item from a list and return it?
- A)
remove() - B)
pop() - C)
delete() - D)
cut()
- A)
- What does the
count()method do?- A) Counts the total memory bytes consumed by the list
- B) Returns the number of times a specified element appears in the list
- C) Counts the number of sublists within the list
- D) Returns the total number of characters in all the elements of the list
- Which method would you use to reverse the order of elements in a list in place?
- A)
reverse() - B)
reversed() - C)
invert() - D)
backwards()
- A)
- If you want to find the largest number in a list of integers, which function would you use?
- A)
max() - B)
min() - C)
sum() - D)
sort()
- A)
- What is the result of
list("hello")?- A)
['h', 'e', 'l', 'l', 'o'] - B)
['hello'] - C)
['h', 'e', 'l', 'o'] - D)
['e', 'l']
- A)
- How do you check if two lists
l1andl2are equal?
- A)
l1 == l2 - B)
l1.equals(l2) - C)
compare(l1, l2) - D)
l1 and l2
- Which method adds elements of a list to another list?
- A)
append() - B)
extend() - C)
insert() - D)
add()
- What will
mylist[2:5]return ifmylist = [10, 20, 30, 40, 50, 60]?
- A)
[30, 40, 50] - B)
[20, 30, 40] - C)
[30, 40] - D)
[20, 30, 40, 50]
- What does the
sort()method do to the list?
- A) Sorts the list in descending order by default
- B) Sorts the list in ascending order by default
- C) Shuffles the list randomly
- D) Reverses the list
- Which line of code will cause an error?
- A)
nums = list("1234") - B)
nums = list(1234) - C)
nums = [1, 2, 3, 4] - D)
nums = list((1, 2, 3, 4))
- Which of the following is not a valid list?
- A)
a = [1, 2, 3] - B)
b = (1, 2, 3) - C)
c = [1, [2, 3], [4, 5]] - D)
d = []
- How do you access the first element of a list named ‘items’?
- A)
items(0) - B)
items[1] - C)
items[0] - D)
first(items)
- Which function will give the total sum of the numeric list
numbers?
- A)
sum(numbers) - B)
total(numbers) - C)
add(numbers) - D)
summarize(numbers)
- What will be the index of the first occurrence of 2 in the list
[1, 2, 3, 2, 2]?
- A)
0 - B)
1 - C)
2 - D)
3
- Which list method is used to remove an item by its value?
- A)
delete() - B)
remove() - C)
pop() - D)
discard()
- How can you combine two lists
list1andlist2?
- A)
list1 + list2 - B)
list1.append(list2) - C)
list1.extend(list2) - D)
list1 and list2
- What does
mylist[:3]return ifmylist = [0, 1, 2, 3, 4, 5]?
- A)
[0, 1, 2] - B)
[1, 2, 3] - C)
[0, 1, 2, 3] - D)
[1, 2, 3, 4]
- How can you slice a list to return every second item from the list
mylist?
- A)
mylist[::2] - B)
mylist[2::] - C)
mylist[:2] - D)
mylist[2]
- What will
['a', 'b', 'c', 'd', 'e'][1:-1]produce?
- A)
['b', 'c', 'd'] - B)
['a', 'b', 'c', 'd'] - C)
['c', 'd', 'e'] - D)
['b', 'c']
- If
data = [10, 20, 30, 40, 50], what willdata[-2:]return?
- A)
[30, 40, 50] - B)
[40, 50] - C)
[10, 20] - D)
[20, 30]
- To reverse the list using slicing, what should you do?
- A)
list[::-1] - B)
list[1::-1] - C)
list[::-2] - D)
list[1::1]
- Which slice operation returns the sublist without the first and last elements?
- A)
mylist[1:-1] - B)
mylist[:-1] - C)
mylist[1:] - D)
mylist[::1]
- What will
mylist[3:6]return ifmylist = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']?
- A)
['d', 'e', 'f'] - B)
['c', 'd', 'e'] - C)
['d', 'e', 'f', 'g'] - D)
['e', 'f', 'g']
- How can you slice a list to exclude the last item?
- A)
list[:-1] - B)
list[-1:] - C)
list[1:-1] - D)
list[-1]
- Which slicing operation includes only the first three elements of a list?
- A)
list[3:] - B)
list[:3] - C)
list[0:3] - D)
list[-3:]
- What does
list[2:-2]do?
- A) Returns all elements except the first two and last two
- B) Returns only the middle two elements
- C) Returns the

