'iterable in Python list comprehension
If I have:
usernames = [user.get('username') for user in json.loads(users)]
Will the json.loads be called once as in normal for loop or many times on each iteration?
Solution 1:[1]
Don't worry: it is called only once, although the language reference isn't exactly clear on this point:
The comprehension consists of a single expression followed by at least one for clause and zero or more for or if clauses. In this case, the elements of the new container are those that would be produced by considering each of the
fororifclauses a block, nesting from left to right, and evaluating the expression to produce an element each time the innermost block is reached.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
