Call int function on every list element?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Converting every element of a list to an integer is one of the most common Python operations — typically needed when reading data from files, user input, or APIs where numbers arrive as strings. Python provides several approaches: map(), list comprehensions, and loops, each with different readability and performance characteristics.
Method 1: map() Function
The most Pythonic approach for applying a single function to every element:
map(int, iterable) returns a lazy iterator, so wrap it in list() if you need a list.
Method 2: List Comprehension
More readable and flexible:
List comprehensions are preferred when you need to add conditions or transformations:
Method 3: For Loop
Explicit but verbose:
Use a loop when you need error handling per element:
Common Use Cases
Reading Numbers from Input
Reading from CSV
Converting Float Strings to Int
Converting Different Bases
Applying Other Functions
The same patterns work for any function, not just int():
Performance Comparison
map() is typically 10-20% faster than list comprehensions for simple function calls because it avoids the overhead of the Python for loop.
NumPy Alternative
For large numeric datasets, NumPy is significantly faster:
Common Pitfalls
- ValueError on non-numeric strings:
int('hello')raisesValueError. Use try/except or validate withstr.isdigit()before converting. - int('') fails: Empty strings raise
ValueError. Filter them out:[int(x) for x in data if x]. - int() truncates floats:
int(3.9)returns3, not4. Useround()if you want rounding. - map() returns an iterator: In Python 3,
map()returns a lazy iterator, not a list. Wrap inlist()if you need indexing or multiple iterations. - Negative number strings:
int('-5')works correctly, but'-5'.isdigit()returnsFalse. Use try/except for robust validation instead ofisdigit().
Summary
- Use
list(map(int, iterable))for the fastest, cleanest conversion of all elements - Use
[int(x) for x in iterable]for readability and when adding conditions - Use a for loop with try/except when error handling is needed per element
map()is ~10-20% faster than list comprehensions for simple function application- For large numeric data, use
np.array(data, dtype=int)for best performance
Related reading
- Can a Byte Array be written to a file in C?
- Can an array be grouped more efficiently than sorted?
- Can anyone explain the deletion of Left-Lean-Red-Black tree clearly?
- Can Dijkstra's Algorithm work on a graph with weights of 0?
- Call predict function for nearest neighbor knn classifier with Python scikit sklearn
- Calling a base class's classmethod in Python
- Can Dijkstra's Single Source Shortest Path Algorithm detect an infinite cycle in a graph?
- Can hash tables really be O1?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.