Iterating Over Data in Various Ways
This example demonstrates different methods for iterating over data in Python. We'll use a sample data structure called users to illustrate these methods.
Introduction
The users data structure contains a list of user objects, each with an ID, login, and a list of phones. The goal is to extract and process phone information from these user objects using various iteration techniques.
users = [
{
"id": 1,
"login": "Alice",
"phones": [
{"type": "mobile", "number": "+420721234567"},
{"type": "home", "number": "+420722345678"},
],
},
{
"id": 2,
"login": "Bob",
"phones": [{"type": "mobile", "number": "+420723456789"}],
},
]
Iterating Over Data
# Create an empty list to store phone information
phones = []
# Iterate over each user
for user in users:
# Log an info message
log.info(f"Checking phones from {user['login']}...")
# Iterate over each phone in the user's phones list
for phone in user["phones"]:
# Log info about the phone
log.info(f"Found phone {phone['number']} (login: {user['login']})")
# Append phone info to the phones list
phones.append({"login": user["login"], "phone": phone["number"]})
return phones # Return the list of phones
import itertools
def functional_iter():
# Use map to extract phones from users, after map we have here a 2D list
phones = map(extract_phones, users)
# Flatten the list of phone lists
phones = itertools.chain.from_iterable(phones)
# Convert the flattened iterator to a list
phones = list(phones)
return phones
def extract_phones(user):
# Log an info message
log.info(f"Checking phones from {user['login']}...")
# Extract and process phones
return map(lambda phone: flatten_phones(user["login"], phone), user["phones"])
def flatten_phones(login, phone):
# Log info about the phone
log.info(f"Found phone {phone['number']} (login: {login})")
# Return phone info as a dictionary
return {"login": login, "phone": phone["number"]}
# Execute the functional iteration and return the result
return functional_iter()
# List Comprehensions
phones = [
{"login": user["login"], "phone": phone["number"]}
for user in users
for phone in user["phones"]
]
# Log the info for each phone found
[log.info(f"Found phone {phone['phone']} (login: {phone['login']})") for phone in phones]
return phones # Return the list of phones
Conclusion
This example provides insight into multiple methods, including list comprehensions, for iterating over data in Python. Whether you prefer traditional for loops, functional programming techniques, or concise comprehensions, Python offers versatile options to process data efficiently based on your specific requirements.