Reviewed by Aditya Kumar · Last reviewed 2026-08-08
The most Pythonic and efficient way to replace characters in a list, except for a specified one, is using a list comprehension. This approach directly constructs a new list by iterating once over the…
This medium-level Python/Coding question appears frequently in data engineering interviews at companies like JP Morgan. While less common, it tests deeper understanding that distinguishes strong candidates. Mastering the underlying concepts (join) will help you answer variations of this question confidently.
Break this problem into components. Identify the core trade-offs involved, then walk the interviewer through your reasoning step by step. Demonstrate awareness of edge cases and production considerations - this is what separates good answers from great ones. The expert answer includes a code example that demonstrates the implementation pattern.
The most Pythonic and efficient way to replace characters in a list, except for a specified one, is using a list comprehension. This approach directly constructs a new list by iterating once over the original, applying a conditional replacement for each element.
A list comprehension [expression_if_true if condition else expression_if_false for item in iterable] is a concise and highly optimized construct in Python for creating new lists. For this problem, the structure [c if c == keep else rep for c in lst] iterates through each character c in the input lst. If c matches the keep character, c itself is retained in the new list. Otherwise, the rep (replacement) character is used. This method is preferred over explicit for loops with append calls because it's often more readable, less prone to off-by-one errors, and frequently performs better due to internal C optimizations. If the input lst is a string, you can convert it to a list first (list(my_string)) and then use str.join() on the result to get a string back.
Consider lst = ['a', 'b', 'a', 'c'], keep = 'a', and rep = ''. The list comprehension processes each element: 'a' (kept), 'b' (replaced with ''), 'a' (kept), 'c' (replaced with ''), yielding ['a', '', 'a', '*']. This solution gracefully handles edge cases such as an empty input list (returning an empty list) or when the keep character is not present (all characters are replaced). A key benefit is that it creates a new list, ensuring the original list remains immutable. The primary trade-off is memory usage, as a new list of roughly the same size as the input is generated.
def replace_except(lst, keep, rep='*'):
return [c if c == keep else rep for c in lst]
Discuss the time complexity as O(N) because the function iterates through the list once. The space complexity is also O(N) as a new list is created to store the results. For extremely large datasets where memory is a concern and the full list isn't needed immediately, you could suggest a generator expression instead of a list comprehension, which would provide O(1) space complexity until iterated over.
Red Flag: Mutating in loop without considering index. Pro-Move: 'We use for PII masking—keep first/last char, replace rest.'
Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.
According to DataEngPrep.tech, this is one of the most frequently asked Python/Coding interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.