Viphawee Wannarungsee
Viphawee Wannarungsee

Reputation: 25

How to access an element from a function output in a list?

I want to access the value of 'zipcode' in the output that's being returned as part of a predefined function called uszipcode. But I am not sure how do I go about it? Any ideas?

from uszipcode import SearchEngine
search = SearchEngine(simple_zipcode=False)
result = search.by_coordinates(44.102665, -121.300797, radius=10, returns=5)
print(result)

The output comes out like this:

[SimpleZipcode(zipcode='97701', zipcode_type='Standard', major_city='Bend', post_office_city='Bend, OR', common_city_list=['Bend'], county='Deschutes County', state='OR', lat=44.0, lng=-121.3, timezone='Pacific', radius_in_miles=37.0, area_code_list=['458', '541'], population=58993, population_density=87.0, land_area_in_sqmi=678.9, water_area_in_sqmi=5.25, housing_units=27682, occupied_housing_units=24589, median_home_value=285300, median_household_income=53444, bounds_west=-121.977954, bounds_east=-120.747881, bounds_north=44.328125, bounds_south=43.752828)]

I want to access the value of zipcode inside SimpleZipcode which is '97701'.

Upvotes: 2

Views: 127

Answers (3)

Ziv Yatziv
Ziv Yatziv

Reputation: 143

Keep in mind that since you're basically searching or filtering, you could possibly get a handful of results. Your query returns a list (or rather an array) of zipcodes.

Since your specific query returns a list that contains one object, you could simply get it by: print(result[0].zipcode)

Read more about it in the documentation: https://uszipcode.readthedocs.io/index.html#example-usage

Search by Latitude and Longitude You can search all zipcode with-in range of XXX miles from a coordinate. You can add returns=xxx to set maximum number of zipcode can be returned. By default, it’s 5. Use returns=0 to remove the limit. The results are sorted by the distance from the center, from lowest to highest.

result = search.by_coordinates(39.122229, -77.133578, radius=30)

len(res) # by default 5 results returned

5

for zipcode in result:

... # do whatever you want...

result = search.by_coordinates(39.122229, -77.133578, radius=100, returns=None)

len(result) # the return limit is removed 3531

Upvotes: 1

shikai ng
shikai ng

Reputation: 135

Seems like results is an array of SimpleZipcode objeact. So you can probably access the zipcode you are interested via

result[0].zipcode

This returns the very first zipcode item in the array of zipcodes returned

Upvotes: 1

Juan
Juan

Reputation: 117

result=result.values() should change the string to a list

so something like

result=result.values()
zipcode=result[0]

should get it done

Upvotes: 0

Related Questions