Python | Convert location coordinates to tuple
Sometimes, while working with locations, we need a lot of data which has location points in form of latitutes and longitudes. These can be in form of a string and we desire to get tuple versions of same. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using tuple() + float() + split() + map()
The combination of above functions can be used to perform this task. In this, we first split the two parts of coordinates into a list, apply float function to each of them using float() and map() and lastly it is converted to tuple using tuple().
# Python3 code to demonstrate working of # Convert location coordinates to tuple # Using tuple() + float() + split() + map() # Initializing string test_str = "44.6463, -49.583" # printing original string print("The original string is : " + str(test_str)) # Convert location coordinates to tuple # Using tuple() + float() + split() + map() res = tuple(map(float, test_str.split(', '))) # printing result print("The coordinates after conversion to tuple are : " + str(res)) |
The original string is : 44.6463, -49.583 The coordinates after conversion to tuple are : (44.6463, -49.583)
Method #2 : Using eval()
This is the one-liner and recommended method to perform this particular task. In this, the eval(), internally detects the string and converts to floating point number seperated as tuple elements.
# Python3 code to demonstrate working of # Convert location coordinates to tuple # Using eval() # Initializing string test_str = "44.6463, -49.583" # printing original string print("The original string is : " + str(test_str)) # Convert location coordinates to tuple # Using eval() res = eval(test_str) # printing result print("The coordinates after conversion to tuple are : " + str(res)) |
The original string is : 44.6463, -49.583 The coordinates after conversion to tuple are : (44.6463, -49.583)
Recommended Posts:
- Python | Reverse Geocoding to get location on a map using geographic coordinates
- Python | Convert a list into a tuple
- Python | Convert tuple to float value
- Python | Convert a list into tuple of lists
- Python | Convert list of tuple into dictionary
- Python | Program to convert a tuple to a string
- Python | Convert tuple records to single string
- Python | Convert list to indexed tuple list
- Python | Convert Integral list to tuple list
- Python | Sort tuple list by Nth element of tuple
- Python | Replace tuple according to Nth tuple element
- Python | Calculate geographic coordinates of places using google geocoding API
- Python script to open a Google Map location on clipboard
- Statistical Functions in Python | Set 1 (Averages and Measure of Central Location)
- Unpacking a Tuple in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



