Changing Types in a List
suggest changeQuantitative data is often read in as strings that must be converted to numeric types before processing. The types of all list items can be converted with either a list comprehension or map(). In Python 3, map returns an iterator, so wrap it in list when you need a list.
# Convert a list of strings to integers.
items = ["1","2","3","4"]
[int(item) for item in items]
# Out: [1, 2, 3, 4]
# Convert a list of strings to float.
items = ["1","2","3","4"]
list(map(float, items))
# Out: [1.0, 2.0, 3.0, 4.0]
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents