Lists — Operations & Methods
Lists are the most-used Python collection in data work. You will use them to store column names, filter conditions, and results from loops. Interviewers test lists heavily.
How do you create a list and access elements?
cols = ["name", "age", "salary"]
cols[0] # "name"
cols[-1] # "salary" (last element)
cols[1:3] # ["age", "salary"] (slicing)Negative indexing (-1 for last) and slicing are Python-specific. In data work, you frequently build lists of column names to select or drop from a DataFrame.
How do you add and remove items from a list?
lst = [1, 2, 3]
lst.append(4) # [1, 2, 3, 4]
lst.extend([5, 6]) # [1, 2, 3, 4, 5, 6]
lst.insert(0, 0) # [0, 1, 2, 3, 4, 5, 6]
lst.remove(3) # removes first occurrence of 3
lst.pop() # removes and returns last itemappend() adds one item; extend() adds multiple. pop() is useful when processing items one by one. remove() deletes by value, pop() by position.
How do you sort a list?
nums = [3, 1, 4, 1, 5, 9]
nums.sort() # sorts in place: [1, 1, 3, 4, 5, 9]
sorted(nums) # returns new sorted list
nums.sort(reverse=True) # descending.sort() modifies the original list. sorted() returns a new one and works on any iterable. In data analysis, you often sort value lists before mapping to charts.
What is a list comprehension and when do you use it?
# Without comprehension:
squares = []
for x in range(5):
squares.append(x**2)
# With list comprehension (preferred):
squares = [x**2 for x in range(5)] # [0, 1, 4, 9, 16]
# With condition:
evens = [x for x in range(10) if x % 2 == 0]List comprehensions are faster and more Pythonic. Interviewers love asking this. Use them to transform or filter lists in one line — e.g. [c.upper() for c in col_names].
How do you find the length, min, max, and sum of a list?
nums = [10, 20, 30, 40]
len(nums) # 4
min(nums) # 10
max(nums) # 40
sum(nums) # 100These built-ins work on any iterable. Before Pandas, these are your quick stats on a raw list. They also work on Pandas Series.
How do you check if an item exists in a list?
cols = ["name", "age", "salary"]
"age" in cols # True
"gender" in cols # FalseThe in operator for lists has O(n) time complexity — it scans every element. For large collections with frequent lookups, use a set instead, which is O(1).
EVIKA ACADEMY · PYTHON FOR DATA ANALYTICS
Want to master Python with live practice?
Join our Python for Data Analysis course — live classes in Noida and online across India.
Book Free Demo Class →