3

I have this code

from opensky_api import OpenSkyApi

api = OpenSkyApi()
states = api.get_states(bbox=(51.3500, 51.5900, -0.6342, -0.2742))

for s in states.states:
    lat = s.latitude
    print(lat)

and the output looks like this

51.4775
51.4589
51.4774
51.4774

how do I make the output look like this?

[51.4775, 51.4589, 51.4774, 51.4774]
Thomas Kimber
  • 9,273
  • 2
  • 23
  • 37

3 Answers3

9
lats = [s.latitude for s in states.states]
print(lats)
Druta Ruslan
  • 6,597
  • 2
  • 25
  • 35
4

try this:

from opensky_api import OpenSkyApi

api = OpenSkyApi()
states = api.get_states(bbox=(51.3500, 51.5900, -0.6342, -0.2742))

arr = []
for s in states.states:
    arr.append(s.latitude)

print(arr)
Ali Yılmaz
  • 1,624
  • 1
  • 10
  • 27
2

Here's a functional method:

from operator import attrgetter

res = list(map(attrgetter('latitude'), states.states))
jpp
  • 147,904
  • 31
  • 244
  • 302