Search code examples
pythonunique

How to Get Unique Latitude and Longitude Pair from List of Dictionary without Numpy?


I have this list of dict:

position_list = [
   {'position.latitude': -1, 'position.longitude': -1}, 
   {'position.latitude': -3, 'position.longitude': 2}, 
   {'position.latitude': -3, 'position.longitude': 2}, 
   {'position.latitude': -4, 'position.longitude': 5},
   {'position.latitude': 5, 'position.longitude': -4}
]

What I want is this:

position_list = [
   {'position.latitude': -1, 'position.longitude': -1}, 
   {'position.latitude': -3, 'position.longitude': 2}, 
   {'position.latitude': -4, 'position.longitude': 5}
   {'position.latitude': 5, 'position.longitude': -4}
]

I can't use the NumPy library because I have to do this not only in Python. How do I achieve this?

EDIT The order should be preserved.


Solution

  • Simplest approach

    >>> new = []
    >>> for i in position_list:
    ...     if i not in new:
    ...         new.append(i)
    ...
    
    >>> new
    [{'position.latitude': -1, 'position.longitude': -1}, {'position.latitude': -3, 'position.longitude': 2}, {'position.latitude': -4, 'position.longitude': 5}, {'position.latitude': 5, 'position.longitude': -4}]
    

    Update: Order is preserved.