python Interview Questions
(4 questions)
Q1. Remove duplicates from a list (order doesn't matter vs order matters)
“Removing duplicates depends on whether we need to preserve the original order or not.”
-
Order doesn’t matter (fastest approach)
- Use set(), which automatically removes duplicates
- But it does not preserve order
nums = [1, 2, 2, 3, 4, 4] unique_nums = list(set(nums)) -
Order matters (preserve original order)
-
Using dict.fromkeys() (best modern approach)
- Removes duplicates while keeping order (Python 3.7+)
unique_nums = list(dict.fromkeys(nums)) -
Using loop (interview-friendly approach)
- Manually check and append only unique values
unique_nums = [] for n in nums: if n not in unique_nums: unique_nums.append(n)
-
“So in simple terms, if order doesn’t matter I use set(), and if order matters I prefer dict.fromkeys() or a loop depending on the situation.”
Q2. How do you iterate and print all key-value pairs in a dictionary?
“To iterate over key-value pairs in a dictionary, the most common and preferred way is using the .items() method.”
-
Using .items() (best approach)
- Returns both key and value together
- We can directly unpack them in the loop
data = {"name": "Prabhu", "age": 25, "city": "Bangalore"} for key, value in data.items(): print(key, value) -
Using .keys() (less preferred)
- Iterates over keys, then access values manually
for key in data.keys(): print(key, data[key]) -
Default iteration (same as keys)
- Directly iterates over keys
for key in data: print(key, data[key])
“So in interviews, I prefer .items() because it’s more readable and directly gives both key and value.”
Q3. Explain lambda functions with examples
“A lambda function is a small anonymous function written in a single line, mainly used for short and simple operations.”
-
Syntax
lambda arguments: expression -
Simple example
add = lambda a, b: a + b print(add(2, 3)) -
Common use cases
- With map() → apply operation to each element
nums = [1, 2, 3, 4] squares = list(map(lambda x: x * x, nums))- With filter() → filter elements based on condition
nums = [1, 2, 3, 4, 5] even = list(filter(lambda x: x % 2 == 0, nums))- With sorted() → custom sorting
data = [(1, 3), (2, 1), (4, 2)] sorted_data = sorted(data, key=lambda x: x[1])
“In simple terms, lambda functions are one-line functions without a name, used for quick operations where defining a full function is not required.”
Q4. How do you read and write JSON and CSV files in Python?
“In Python, we use built-in libraries like json and csv, and in real projects, pandas is the most commonly used approach.”
-
JSON files
- Read JSON
import json with open("data.json", "r") as file: data = json.load(file)- Write JSON
import json data = {"name": "Rahul", "age": 25} with open("data.json", "w") as file: json.dump(data, file) -
CSV files
- Read CSV
import csv with open("data.csv", "r") as file: reader = csv.reader(file) for row in reader: print(row)- Write CSV
import csv data = [ ["name", "age"], ["Rahul", 25], ["Amit", 30] ] with open("data.csv", "w", newline="") as file: writer = csv.writer(file) writer.writerows(data) -
Using pandas (most common in Data Engineering)
- Easier and more efficient for real-world use
import pandas as pd # Read df = pd.read_csv("data.csv") df_json = pd.read_json("data.json") # Write df.to_csv("output.csv", index=False) df.to_json("output.json", orient="records")
“So in simple terms, for JSON we use json.load() and json.dump(), for CSV we use csv.reader() and csv.writer(), and in real projects we mostly use pandas because it’s more convenient and powerful.”