Questions Related to Interview Questions & Answers
Updated on June 2, 2025 | By Learnzy Academy
Q1. How to check if a tuple is a subset of another tuple in python ?
Use Python’s all() function to test if every element of the first tuple exists in the second tuple.
Example:-
tuple1 = (1, 2, 3)
tuple2 = (5, 3, 2, 1, 4)
is_subset = all(item in tuple2 for item in tuple1)
print(is_subset) # True
Q2. How to find the first and last elements of a tuple in python?
Since tuples are ordered collections, you can access elements by their index. The first element is at index 0. And the last element is at index -1.
Example:--
my_tuple = (10, 20, 30, 40, 50)
first_element = my_tuple[0]
last_element = my_tuple[-1]
print("First element:", first_element)
print("Last element:", last_element)
Q3. How to check if two lists are equal?
To check if two lists are equal (same elements in the same order), use the == operator.
Example :--
list1 = [1, 2, 3]
list2 = [1, 2, 3]
if list1 == list2:
print("Lists are equal")
else:
print("Lists are not equal")
Q4. Find common elements in two lists
Using set intersection we can get the common element b/w two list. This is the fast way to get unique common elements, but order is not preserved
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
common = list(set(list1) & set(list2))
print(common) # Output: [3, 4]
Q5. Is Python list mutable?
Yes, Python lists are mutable.
This means you can change a list after you create it. You can add, remove, or change items in the list.
my_list = [1, 2, 3]
my_list[0] = 10 # change first item
my_list.append(4) # add a new item
print(my_list) # prints [10, 2, 3, 4]
Q6. How to remove duplicates from a list?
Method 1: list(set(my_list))
Removes duplicates, but order is not preserved.
my_list = [1, 2, 2, 3, 1]
unique_list = list(set(my_list))
print(unique_list) # Output: [1, 2, 3] (order may change)Method 2: list(dict.fromkeys(my_list))
Removes duplicates and keeps original order.
my_list = [1, 2, 2, 3, 1]
unique_list = list(dict.fromkeys(my_list))
print(unique_list) # Output: [1, 2, 3]Q7. Difference between sort() and sorted()?
1. sort()
- Method used only on lists
- Sorts the list in place (modifies the original list)
- Does not return the sorted list (returns None)
- More memory efficient because it doesn’t create a new list
numbers = [3, 1, 4, 2] numbers.sort() print(numbers) # Output: [1, 2, 3, 4]
2. sorted()
- Built-in function that works on any iterable (like lists, tuples, strings, sets, etc.)
- Returns a new sorted list
- Does not change the original data
numbers = [3, 1, 4, 2] new_list = sorted(numbers) print(numbers) # Output: [3, 1, 4, 2] print(new_list) # Output: [1, 2, 3, 4]
Q8. What is a list in Python?
A list is a built-in data type in Python that is used to store multiple items in a single variable. It is ordered, mutable, and can contain elements of different data types.
Q9. How to remove an element from a list?
You can remove elements from a list in Python using several methods, depending on how you want to remove them.
1. remove(value)
- Removes the first occurrence of a specific value.
- Raises an error if the value is not found.
a = [1, 2, 3, 2] a.remove(2) print(a) # Output: [1, 3, 2]
2. pop(index)
- Removes and returns the element at the specified index.
- If no index is given, removes the last item.
- Raises an error if the index is out of range.
a = [10, 20, 30] a.pop(1) print(a) # Output: [10, 30]
3. del statement
- Deletes an element by index, or slices of the list.
a = [5, 6, 7, 8] # Delete element by index del a[2] print(a) # Output: [5, 6, 8] # Delete a slice del a[1:3]
Q10. What is the difference between append() and extend()?
append():
- Adds a single item to the end of the list.
- The item can be of any type (including another list).
- If you append a list, it adds the whole list as one element (nested list).
a = [1, 2, 3] a.append([4, 5]) # Result: [1, 2, 3, [4, 5]]
extend():
- Adds each element from an iterable (like a list, tuple, etc.) to the end of the list.
- It "extends" the list by adding elements one by one.
- If you extend with a list, its items are added individually (not nested).
a = [1, 2, 3] a.extend([4, 5]) # Result: [1, 2, 3, 4, 5]