Python Dictionary & List Methods - Output Questions with
Answers
List Methods
• append(): Adds a single element to the end of the list.
Example: lst.append(5)
• extend(): Adds multiple elements from another iterable to the end of the
list.
Example: lst.extend([1, 2])
• insert(): Inserts an element at a specified position.
Example: lst.insert(1, 'a')
• remove(): Removes the first matching element from the list.
Example: lst.remove(2)
• pop(): Removes and returns the element at the given index (default is
last).
Example: lst.pop()
• index(): Returns the index of the first matching element.
Example: lst.index(3)
• count(): Returns the number of occurrences of a value.
Example: lst.count(2)
• sort(): Sorts the list in ascending order (in-place).
Example: lst.sort()
• reverse(): Reverses the list in place.
Example: lst.reverse()
• clear(): Removes all items from the list.
Example: lst.clear()
Dictionary Methods
• get(): Returns the value for a specified key if the key exists.
Example: d.get('a')
• keys(): Returns a view object with all keys in the dictionary.
Example: d.keys()
• values(): Returns a view object with all values in the dictionary.
Example: d.values()
• items(): Returns a view object with key-value pairs.
Example: d.items()
Page 1 of 6
• update(): Updates the dictionary with key-value pairs from another
dictionary.
Example: d.update({'b': 2})
• pop(): Removes the element with the specified key.
Example: d.pop('a')
• popitem(): Removes the last inserted key-value pair.
Example: d.popitem()
• setdefault(): Returns the value of a key. If key not present, inserts it with
a specified value.
Example: d.setdefault('c', 3)
• clear(): Removes all elements from the dictionary.
Example: d.clear()
• copy(): Returns a shallow copy of the dictionary.
Example: d.copy()
List Methods
1. Q: What does the append() method do?
A: It adds an item to the end of the list.
2. Q: How does the extend() method differ from append()?
A: extend() adds elements from another iterable, while append() adds a
single element.
3. Q: What is the purpose of the insert() method?
A: It inserts an item at a specified position in the list.
4. Q: How do you remove an item by value from a list?
A: Use the remove() method.
5. Q: What does pop() do in a list?
A: It removes and returns the item at the specified position (last item if no
index is provided).
6. Q: How can you delete all items in a list?
A: Use the clear() method.
7. Q: Which method returns the index of the first matching item?
A: The index() method.
Page 2 of 6
8. Q: How do you count the number of times a value appears in a list?
A: Use the count() method.
9. Q: What method is used to sort a list?
A: The sort() method.
10.Q: How do you reverse the order of elements in a list?
A: Use the reverse() method.
11.Q: Which method creates a copy of the list?
A: The copy() method.
Dictionary Methods
12.Q: What does the get() method do?
A: Returns the value for a specified key.
13.Q: How do you get all the keys in a dictionary?
A: Use the keys() method.
14.Q: How can you retrieve all values in a dictionary?
A: Use the values() method.
15.Q: Which method gives both keys and values as pairs?
A: The items() method.
16.Q: How do you add or update key-value pairs in a dictionary?
A: Use the update() method.
17.Q: How do you remove a key-value pair by key?
A: Use the pop() method.
18.Q: What is the use of popitem()?
A: It removes and returns the last inserted key-value pair.
19.Q: How can you empty an entire dictionary?
A: Use the clear() method.
20.Q: How do you duplicate a dictionary?
Page 3 of 6
A: Use the copy() method.
21.What will be the output of the following code?
my_list = [1, 2, 3, 4]
my_list.append(5)
print(my_list)
Output: [1, 2, 3, 4, 5]
22.What will be the output of the following code?
my_dict = {'a': 1, 'b': 2}
my_dict.update({'c': 3})
print(my_dict)
Output: {'a': 1, 'b': 2, 'c': 3}
23.What will be the output of the following code?
my_list = [10, 20, 30, 40]
my_list.remove(20)
print(my_list)
Output: [10, 30, 40]
24.What will be the output of the following code?
my_dict = {'x': 10, 'y': 20}
print(my_dict.get('z', 'Not Found'))
Output: Not Found
25.What will be the output of the following code?
my_list = [3, 1, 4, 2]
my_list.sort()
print(my_list)
Output: [1, 2, 3, 4]
26.What will be the output of the following code?
my_dict = {'a': 100, 'b': 200}
print(my_dict.keys())
Output: dict_keys(['a', 'b'])
27.What will be the output of the following code?
my_list = ['a', 'b', 'c']
Page 4 of 6
my_list.insert(1, 'z')
print(my_list)
Output: ['a', 'z', 'b', 'c']
28.What will be the output of the following code?
my_dict = {'one': 1, 'two': 2}
my_dict.pop('one')
print(my_dict)
Output: {'two': 2}
29.What will be the output of the following code?
my_list = [5, 6, 7]
my_list.clear()
print(my_list)
Output: []
30.What will be the output of the following code?
my_dict = {'a': 1, 'b': 2}
print('c' in my_dict)
Output: False
# Dictionary methods
my_dict = {"a": 1, "b": 2, "c": 3}
print(my_dict.get("b")) # Output: 2
print(my_dict.get("d", 0)) # Output: 0
print(my_dict.keys()) # Output: dict_keys(['a', 'b', 'c'])
print(my_dict.values()) # Output: dict_values([1, 2, 3])
print(my_dict.items()) # Output: dict_items([('a', 1), ('b', 2), ('c', 3)])
my_dict.pop("b")
print(my_dict) # Output: {'a': 1, 'c': 3}
my_dict.update({"d": 4})
print(my_dict) # Output: {'a': 1, 'c': 3, 'd': 4}
Page 5 of 6
# List methods
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]
my_list.insert(0, 0)
print(my_list) # Output: [0, 1, 2, 3, 4]
my_list.remove(2)
print(my_list) # Output: [0, 1, 3, 4]
my_list.pop(1)
print(my_list) # Output: [0, 3, 4]
Page 6 of 6