CarterB
CarterB

Reputation: 542

Filter a dictionary to return certain keys

I have been trying to look at other questions and not found an answer that sufficiently solves my problem. I have a list if dictionaries that contains many keys, and want to return a list of dictionaries which contain only three of these keys.

dict_keys(['dropoff_datetime', 'dropoff_latitude', 'dropoff_longitude', 'fare_amount', 'imp_surcharge', 'mta_tax', 'passenger_count', 'payment_type', 'pickup_datetime', 'pickup_latitude', 'pickup_longitude', 'rate_code', 'tip_amount', 'tolls_amount', 'total_amount', 'trip_distance', 'vendor_id'])

I Write a function called parse_trips(trips) that returns a list of the trips with only the following attributes:

trip_distance
pickup_latitude
pickup_longitude

I have tried so many iterations of this, and would be happy to use either map or filter, of which i have tried many variations to no success.

def parse_trips(trips):
new_list = []
for trip in trips:
    return trips['trip_distance'], trips['pickup_latidude'], trips['pickup_longitude']

parsed_trips = parse_trips(trips)
parsed_trips and parsed_trips[0]

This is the output i'm trying to get

# {'pickup_latitude': '40.64499',
#  'pickup_longitude': '-73.78115',
#  'trip_distance': '18.38'}

Upvotes: 1

Views: 2378

Answers (3)

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20490

You can create a dictionary with needed keys and their respective values via dictionary comprehension and add it to new_list

#List of keys you need
needed_keys = ['trip_distance', 'pickup_latitude', 'pickup_longitude']

#Iterate over trips list
for trip in trips:

    #Create a dictionary with needed keys and their respective values and add it to result
    new_list.append({k:trip[k] for k in needed_keys})

Upvotes: 2

Suuuehgi
Suuuehgi

Reputation: 4990

needed_keys = ['trip_distance', 'pickup_latitude', 'pickup_longitude']

for d in dicts:
    for k in [ key for key in d.keys() if not key in needed_keys ]:
        d.pop( k, None )

MWE

keys = ['dropoff_datetime', 'dropoff_latitude', 'dropoff_longitude', 'fare_amount', 'imp_surcharge', 'mta_tax', 'passenger_count', 'payment_type', 'pickup_datetime', 'pickup_latitude', 'pickup_longitude', 'rate_code', 'tip_amount', 'tolls_amount', 'total_amount', 'trip_distance', 'vendor_id']
needed_keys = ['trip_distance', 'pickup_latitude', 'pickup_longitude']

dicts = []
for i in range(10):
    dicts.append( { i:iii for iii,i in enumerate(keys) } )

for d in dicts:
    for key in [ key for key in d.keys() if not key in needed_keys ]:
        d.pop( key, None )

Upvotes: 0

user3896255
user3896255

Reputation:

You want to make sure that you have all the keys in your target dictionaries, so you can use the builtin all() function to verify that each key in your target list of needed keys exist.

And then you keep only those trips.

After that, you can access any fields from your parsed trips.

needed_keys = ['trip_distance', 'pickup_latitude', 'pickup_longitude']
parsed_trips = [trip for trip in trips if all(key in trip for key in needed_keys)]

If you want to only return those 3 keys in your final list, you can pull them out with:

parsed_trips = [{k: trip[k] for k in needed_keys} for trip in trips if all(key in trip for key in needed_keys)]

Although I think the list/dict comprehensions there start getting a little long and ugly, so you may want to switch to a traditional for loop instead.

As a side note, you could always skip any trips where trip.get('target_field') is None, because the .get() won't throw you an index error, because it defaults to returning None if the key doesn't exist.

Upvotes: 0

Related Questions