-1

I need to send out request one by one by different id and I have following code but I am wondering if it is good idea to use syntax like list(map(lambda x: xxxx), id_list) or i should just use one-for loop for it?

id_list = [1,3,4,100,83,99]
headers = {'content-type': 'application/json'}
url = "https://www.test.com/api/v6/test.json"
response = requests.delete(url, data=json.dumps(payload), headers=headers,auth=HTTPBasicAuth(token, 'api_token'))
list(map(lambda x: requests.delete(url, data=json.dumps({'id': x}), headers=headers,auth=HTTPBasicAuth(token, 'api_token')), id_list))
jacobcan118
  • 7,797
  • 12
  • 50
  • 95
  • I vote for list comprehensions. – YOLO Jan 02 '19 at 22:29
  • 2
    No, this is entirely for a side-effect, to send a request. You should use a regular loop. Don't use functional programming constructs, like `map` or list comprehensions, for side effects! – juanpa.arrivillaga Jan 02 '19 at 22:30
  • @YOLO Abusing list comprehensions for side effect is bad, and anyway OP wrote "without create extra list" – DeepSpace Jan 02 '19 at 22:30
  • The entire purpose of `map` (and generator expressions) is to lazily produce a new list via some mapping function. Not only is this an abuse of `map` as mentioned above, if you forget to force evaluation of the list in the future, the side effects may not run. If you want to simply loop over a list to carry out side effects, use a plain, imperative loop. – Carcigenicate Jan 02 '19 at 22:34

1 Answers1

2

You should use a for loop.

As @juanpa.arrivillaga and @DeepSpace pointed out - using list comprehensions to perform code that has side-effects is probably not a good idea.

Jaime M
  • 161
  • 6
  • so what if the lambda inside the map does not have any side-effect like convert all int element of list into string? `map(int, list)` still bad idea? – jacobcan118 Jan 02 '19 at 22:40
  • @jacobcan118 That is exactly the purpose of `map`, so that would be fine. I believe though that it's generally recommended to use list comprehensions/generator expressions in modern Python instead of `map`. – Carcigenicate Jan 02 '19 at 22:45