I got a list of strings from the REST API. I know from the documentation that an element with index 0 and 2 is an integer, and an element with 1 and 3 is floats.
To perform any calculations with the data, I need to cast them to the appropriate type. Although you can apply values โโevery time they are used, I prefer to distinguish the list to the correct type before starting calculations to clear the equations. The code below works, but is very ugly:
rest_response = ['23', '1.45', '1', '1.54'] first_int = int(rest_response[0]) first_float = float(rest_response[1]) second_int = int(rest_response[2]) second_float = float(rest_response[3])
Since I work with integers and floats in this particular example, one possible solution is to throw each element for a float. float_response = map(float, rest_response) . Then I can simply unzip the list to correctly name the values โโin the equations.
first_int, first_float, second_int, second_float = float_response
This is my current solution (but with better names), but after finding out that one of them I was curious, is there any good pythonic solution to this problem?
source share