DEV Community

Discussion on: How to use Python to sum a list?

Collapse
 
peter279k profile image
peter279k • Edited

Before using the sum function to summarize the list, we can consider using the map to try to cast all values to integer type firstly.

For example, the sample codes are as follows:

numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "10"]

sum_numbers = sum(map(int, numbers))
print(sum_numbers)
Enter fullscreen mode Exit fullscreen mode