🐍2.Python Lists for Interviews_details
Here’s a detailed Python List Guide for Interviews covering key concepts, functions, and coding examples:
1. List Basics
A list is an ordered, mutable collection that can hold different data types.
Syntax:
my_list = [1, 2, 3, 4, 5]
Properties of Lists:
- Ordered (maintains insertion order)
- Mutable (can be modified)
- Allows Duplicates
- Dynamic (can grow/shrink)
2. Creating Lists
1. Using Square Brackets
numbers = [10, 20, 30]
2. Using list() Constructor
numbers = list((10, 20, 30))
3. Empty List
empty_list = []
4. List with Mixed Data Types
mixed_list = [10, "Hello", 3.14, True]
5. List of Lists (Nested List)
nested_list = [[1, 2, 3], [4, 5, 6]]
3. Accessing List Elements
1. Indexing
nums = [10, 20, 30, 40]
print(nums[1]) # Output: 20
print(nums[-1]) # Output: 40 (Last Element)
2. Slicing
nums = [10, 20, 30, 40, 50]
print(nums[1:4]) # Output: [20, 30, 40]
print(nums[:3]) # Output: [10, 20, 30]
print(nums[::2]) # Output: [10, 30, 50] (Step size 2)
4. Modifying Lists
1. Changing Elements
nums = [10, 20, 30]
nums[1] = 50
print(nums) # Output: [10, 50, 30]
2. Adding Elements
append()→ Adds at the endinsert()→ Adds at a specific indexextend()→ Adds multiple elements
nums = [10, 20, 30]
nums.append(40) # [10, 20, 30, 40]
nums.insert(1, 15) # [10, 15, 20, 30, 40]
nums.extend([50, 60]) # [10, 15, 20, 30, 40, 50, 60]
5. Removing Elements
remove(value)→ Removes first occurrence of valuepop(index)→ Removes and returns element at indexdel list[index]→ Deletes element at indexclear()→ Removes all elements
nums = [10, 20, 30, 40]
nums.remove(20) # [10, 30, 40]
nums.pop(1) # [10, 40]
del nums[0] # [40]
nums.clear() # []
6. List Operations
1. Concatenation
list1 = [1, 2]
list2 = [3, 4]
print(list1 + list2) # [1, 2, 3, 4]
2. Repetition
nums = [1, 2] * 3
print(nums) # [1, 2, 1, 2, 1, 2]
3. Membership (in, not in)
nums = [10, 20, 30]
print(20 in nums) # True
print(50 not in nums) # True
7. Iterating Over Lists
1. Using for Loop
nums = [10, 20, 30]
for num in nums:
print(num)
2. Using while Loop
nums = [10, 20, 30]
i = 0
while i < len(nums):
print(nums[i])
i += 1
3. Using enumerate()
nums = [10, 20, 30]
for index, value in enumerate(nums):
print(index, value)
8. List Comprehensions
squares = [x**2 for x in range(5)]
print(squares) # [0, 1, 4, 9, 16]
With Condition:
even_nums = [x for x in range(10) if x % 2 == 0]
print(even_nums) # [0, 2, 4, 6, 8]
9. Sorting and Reversing
1. sort() (Modifies List)
nums = [3, 1, 4, 1, 5]
nums.sort()
print(nums) # [1, 1, 3, 4, 5]
2. sorted() (Returns New List)
nums = [3, 1, 4, 1, 5]
print(sorted(nums)) # [1, 1, 3, 4, 5]
3. reverse()
nums = [1, 2, 3]
nums.reverse()
print(nums) # [3, 2, 1]
10. List Functions
| Function | Description |
|---|---|
len(lst) |
Returns length of the list |
max(lst) |
Returns max value |
min(lst) |
Returns min value |
sum(lst) |
Returns sum of elements |
list.count(value) |
Counts occurrences of value |
list.index(value) |
Returns index of first occurrence of value |
nums = [10, 20, 30, 20]
print(len(nums)) # 4
print(max(nums)) # 30
print(min(nums)) # 10
print(sum(nums)) # 80
print(nums.count(20)) # 2
print(nums.index(30)) # 2
11. Copying Lists (Avoiding Pitfalls)
Incorrect Way (Shallow Copy)
list1 = [1, 2, 3]
list2 = list1 # Both point to the same list!
list2[0] = 99
print(list1) # [99, 2, 3]
Correct Way (copy() or [:])
list1 = [1, 2, 3]
list2 = list1[:] # Creates a new copy
list2[0] = 99
print(list1) # [1, 2, 3]
list1 = [1, 2, 3]
list2 = list1.copy()
12. Nested List Processing
matrix = [[1, 2, 3], [4, 5, 6]]
for row in matrix:
for item in row:
print(item, end=" ")
# Output: 1 2 3 4 5 6
Common Interview Questions
- Find second largest number in a list.
- Remove duplicates from a list.
- Flatten a nested list.
- Rotate a list left or right.
- Find pairs in a list that sum to a given number.
Do you want solutions for any of these? 🚀
Comments
Post a Comment