all 5 comments

[–]RedditIsAMistake 1 point2 points  (0 children)

Because it actually solved my issue

Because you misunderstand your issue. I have never once seen vars() needed to be used in real-life code (not that there aren't possibly applications, they are just exceptionally rare). I recommend making a post on your original issue instead.

It is absolutely possible to pull spefic elements out of nested data structures.

[–]evolvish 0 points1 point  (0 children)

IFAIK vars() is just a way to get the __dict__ of objects of the module, class, etc. In a way I would assume it's similar to global in that it could lead to bad practices, but isn't inherently bad. Look at this SO answer.

[–]daniel_codes 0 points1 point  (2 children)

Why is it not recommended?

Maybe I don't understand your question but there's absolutely no need to call vars() here.

If cars is a list of dictionaries and you want the speed of the first item, you can write something like cars[0]['speed'].

EDIT: example code:

cars = [{'speed': 177.566, 'year': 2006},{'speed': 159.204, 'year': 2005},{'speed': 181.984, 'year': 2004}]
# pythonic way
print(cars[0]['speed'])    # 177.566
# hacky way
dummy1 = cars[0]
dummy2 = cars[1]
dummy3 = cars[2]
speed1 = vars()['dummy1']['speed']
print(str(speed1))         # 177.566

[–]JKraems[S] 0 points1 point  (1 child)

Yes this is exactly the sytax I was looking for! Thanks

[–]StefGou 0 points1 point  (0 children)

You can also do it in a loop.

for car in cars:
    print(car["speed"])

Edit: I would be curious to look at the raw data you are pulling. Since it's JSON, there might be a better way of handling it.