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
  • 10,601
  • 3
  • 25
  • 42

3 Answers3

9
lats = [s.latitude for s in states.states]
print(lats)
Druta Ruslan
  • 7,171
  • 2
  • 28
  • 38
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,657
  • 1
  • 11
  • 28
2

Here's a functional method:

from operator import attrgetter

res = list(map(attrgetter('latitude'), states.states))
jpp
  • 159,742
  • 34
  • 281
  • 339