[Solved] How do I change a list into integers on Python? [closed]


Before anything, make your tuple into a list. A list is easier to work with as it is mutable (modifiable), whereas a tuple is not.

>>> dates = list(('01/01/2009', '02/01/2009', '03/01/2009', '04/01/2009'))
>>> dates
['01/01/2009', '02/01/2009', '03/01/2009', '04/01/2009']

Request number one, strings of integers:

>>> answer1 = [item[:2] for item in dates]  # Here we slice the string.
>>> answer1
['01', '02', '03', '04']

Request number two, floats:

>>> answer2 = [float(item) for item in answer1]   # Conversion is easy as that.
>>> answer2
[1.0, 2.0, 3.0, 4.0]

However the above solutions could be concatenated as such:

>>> final = [float(item[:2]) for item in dates]   # A list comprehension.
>>> final
[1.0, 2.0, 3.0, 4.0]

Request number three, delete first list item:

>>> final.pop(0)  # Removes and returns an item at the specified index.
1.0
>>> final
[2.0, 3.0, 4.0]

5

solved How do I change a list into integers on Python? [closed]