[Solved] Looping through array of objects in Python coming from Javascript


contacts = [{"name":"John","age":30},{"name":"Peter","age":20},{"name":"Sarah","age":33}]

for i in range(len(contacts)):
    print(contacts[i]["name"])
    
for person in contacts:
    print(person["name"])

The second way would be considered more “Pythonic”.

EDIT – added to answer question in comment

To access only the last record in the list, use a negative offset.

print(contacts[-1]["name"])

1

solved Looping through array of objects in Python coming from Javascript