0

How can I return a subset of the data dictionary with dict comprehension?

for example

from urllib.parse import urlencode, urlparse

data = [{"id": 123, "description": "big one", "productId": 1}, {"id":124, "description": "medium one", "productId": 2}, {"id": 125, "description": "small one", "productId": 3}]

url = urlparse("https://example.api.com/")
for elem in data:
    query = urlencode(
        query={"description": elem.get("description"), "productId": elem.get("productId")}
    )
    path = f"v1/product/{elem['id']}"
    newurl = url._replace(path=path, query=query)
    logger.debug(newurl.geturl())

Progman
  • 16,827
  • 6
  • 33
  • 48
izzleee
  • 315
  • 3
  • 11

1 Answers1

0

My question is poorly worded. I was hoping to get a subset of a dictionary using keys. Answer is here: Extract subset of key-value pairs from dictionary?

izzleee
  • 315
  • 3
  • 11