0

This is part of the bigger data set but if the code works I'm sure I could apply it to the entire data set. Here's the data sample I am working with.

data = [
    {
        'listing_id': '1133718',
        'survey_id': '1280',
        'host_id': '6219420',
        'room_type': 'Shared room',
        'country': '',
        'city': 'Singapore',
        'borough': '',
        'neighborhood': 'MK03',
        'reviews': 9.0,
        'overall_satisfaction': 4.5,
        'accommodates': '12',
        'bedrooms': '1.0',
        'bathrooms': '',
        'price': 74.0,
        'minstay': '',
        'last_modified': '2017-05-17 09:10:25.431659',
        'latitude': 1.293354,
        'longitude': 103.769226,
        'location': '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F'
    },
    {
        'listing_id': '1196810',
        'survey_id': '1280',
        'host_id': '6236420',
        'room_type': 'Shared room',
        'country': '',
        'city': 'Singapore',
        'borough': '',
        'neighborhood': 'MK11',
        'reviews': 9.0,
        'overall_satisfaction': 3.5,
        'accommodates': '11',
        'bedrooms': '2.0',
        'bathrooms': '',
        'price': 84.0,
        'minstay': '',
        'last_modified': '2017-05-17 09:10:25.431659',
        'latitude': 1.34567,
        'longitude': 103.769226,
        'location': '0101000020E6100000E84EB0FF3AF159409C69C2F693B1F43F'
    }
    .
    .
    .
    ]

If the function works, I want to trigger a function like:

get_all_latitude(data, ['1196810', '1133718'])

with the expected output:

 [1.34567, 1.293354]
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
OnionCoder
  • 87
  • 7
  • Hi, thanks for your help. I'm sorry but I'm kinda still a beginner so is it possible to suggest a simpler code for me to understand? Thanks – OnionCoder Jul 27 '19 at 03:25
  • Both answers below work. The answer from Alex is fairly straightforward. If that isn't making sense you may want to read up on conditional list comprehension. https://stackoverflow.com/questions/4260280/if-else-in-a-list-comprehension –  Jul 27 '19 at 04:51

2 Answers2

3

I would do this: (This assumes that the 'data' variable is a list, which I assume is what you intended.)

def get_all_latitude_data(data, ids):
    return [datum["latitude"] for datum in data if datum["listing_id"] in ids]

This will do as expected. If there are no data points whose listing_ids are in the list, it will return an empty list.

------------------------EDIT -----------------------

Following your comment on the question, if it helps, this list comprehension is basically doing this:

def get_all_latitude_data_loop(data, ids):
    output = []
    for datum in data:
        if datum["listing_id"] in ids:
            output.append(datum["latitude"])
    return output

Bear in mind, the list comprehension is, IMHO, more 'pythonic', and usually faster, but both work.

Alex
  • 2,270
  • 3
  • 33
  • 65
1

Instead of searching every time, just store it in key-value pair (listing_id and latitude) in dict and using for loop just retrieve the latitude value for given listing_id and solve it

def get_all_latitude(data, list_of_data):
    dic = {i['listing_id']:i['latitude'] for i in data}
    return [ dic[i] for i in list_of_data ]   

list_of_data = ['1196810', '1133718']
print(get_all_latitude(data,list_of_data))

output

[1.34567, 1.293354]
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • This seems like a pretty expensive solution to me compared to the one that @Alex suggested. Each time you call `get_all_latitude` you recreate that dictionary just so you can look up the data when you could just do something with them while you're iterating over them, rather than creating an additional data structure in memory. – darth_mall Jul 27 '19 at 17:32
  • @darth_mall yes you are right this is slow, compare to alex, but you can use memorization to overcome that – sahasrara62 Jul 27 '19 at 21:06