Processing API Data with `requests` and Lambdas in Python
Python’s requests library makes HTTP calls straightforward, while small lambda functions can be useful as transformation or sorting keys. This article shows how to combine them without turning simple data processing into hard-to-read one-liners.
Install requests
python -m pip install requestsFetch JSON from an API
import requests
response = requests.get(
"https://jsonplaceholder.typicode.com/posts",
timeout=10,
)
response.raise_for_status()
data = response.json()Two details matter here:
timeout=10prevents the request from waiting indefinitely.raise_for_status()raises an exception for unsuccessful HTTP status codes.
Extract Values
A lambda can be passed to map():
titles = list(map(lambda post: post["title"], data))But a list comprehension is usually clearer in Python:
titles = [post["title"] for post in data]Use a lambda when an API specifically expects a callable and the expression is short.
Filter Results
filter() accepts a callable:
user_posts = list(filter(lambda post: post["userId"] == 1, data))Again, a comprehension is often easier to read:
user_posts = [post for post in data if post["userId"] == 1]Sort API Data with a Lambda
Sorting keys are one of the most natural uses of lambdas:
sorted_posts = sorted(data, key=lambda post: post["title"])For descending order by ID:
sorted_posts = sorted(data, key=lambda post: post["id"], reverse=True)Handle Request Errors
import requests
try:
response = requests.get(
"https://jsonplaceholder.typicode.com/posts",
timeout=10,
)
response.raise_for_status()
data = response.json()
except requests.RequestException as exc:
print(f"Request failed: {exc}")JSON decoding can also fail if a server returns an unexpected body, so production code should decide how that condition should be handled.
Keep Lambdas Small
This is concise:
users.sort(key=lambda user: user["name"].casefold())If the transformation needs validation, branching, logging, or multiple steps, define a normal function instead:
def normalized_name(user):
name = user.get("name", "")
return name.casefold()
users.sort(key=normalized_name)Conclusion
Use requests with explicit timeouts and status checking when consuming HTTP APIs. Lambdas work best as short callback expressions, especially sorting keys. For filtering and transformation, Python comprehensions are often more readable than map() or filter() with lambdas.