Iterate through array of object in Python
- Get link
- X
- Other Apps
Iterate through array of object in Python
Introduction: Arrays of objects are a common data structure in programming, especially in Python. These arrays are collections of objects that can be iterated over to access or manipulate the objects. In this blog, we will explore how to iterate through an array of objects in Python, including how to access the objects, modify them, and perform operations on them.
Iterating through an array of objects: Iterating through an array of objects in Python is similar to iterating through any other collection of data. We can use a for loop to iterate through each object in the array, and access or manipulate the properties of each object as needed. Here's an example:
students = [ {"name": "John", "age": 25}, {"name": "Jane", "age": 30}, {"name": "Bob", "age": 20} ] for student in students: print(student["name"], student["age"])
In this example, we have an array of objects representing students, with each object containing a name and age property. We use a for loop to iterate through each object in the array, and access the name and age properties of each object using the square bracket notation.
Modifying objects in an array: Iterating through an array of objects also allows us to modify the properties of each object as needed. Here's an example:
students = [ {"name": "John", "age": 25}, {"name": "Jane", "age": 30}, {"name": "Bob", "age": 20} ] for student in students: student["age"] += 1 print(students)
In this example, we use a for loop to iterate through each object in the array of students, and increment the age property of each student by 1. We then print out the updated array of students.
Performing operations on objects in an array: Iterating through an array of objects also allows us to perform operations on each object as needed. Here's an example:
students = [ {"name": "John", "age": 25}, {"name": "Jane", "age": 30}, {"name": "Bob", "age": 20} ] total_age = 0 for student in students: total_age += student["age"] average_age = total_age / len(students) print("Average age:", average_age)
In this example, we use a for loop to iterate through each object in the array of students, and calculate the total age of all students. We then divide the total age by the number of students in the array to calculate the average age of all students. Finally, we print out the average age.
Conclusion: Iterating through an array of objects in Python is a common task that allows us to access, modify, and perform operations on the objects in the array. By using a for loop, we can easily iterate through each object in the array and access its properties or perform operations on it. Whether you are working with arrays of students, employees, or any other object, the concepts and techniques discussed in this blog can help you work with arrays of objects in Python with ease.
To loop through an array of objects in a JSON string using Python, you can parse the JSON string using the json
module and then use a for loop to iterate over the resulting list of dictionaries.
Here's an example:
import json json_string = '[{"name": "John", "age": 30}, {"name": "Jane", "age": 25}, {"name": "Bob", "age": 40}]' data = json.loads(json_string) for item in data: print(item['name'], item['age'])
In this example, we first import the json
module and define a JSON string with an array of objects. We then use the json.loads()
method to parse the JSON string into a Python list of dictionaries.
Next, we use a for loop to iterate over the list of dictionaries. For each dictionary, we print out the values of the name
and age
keys.
This code will output:
John 30 Jane 25 Bob 40
item
variable in the for loop represents each dictionary in the list, so you can access the values of each key using the item[key]
syntax.Happy Learning!! Happy Coding!!
- Get link
- X
- Other Apps
Comments