list-comprehension syntax for functions

python list comprehension multiple conditions
list comprehension python for loop
nested list comprehension python
list comprehension python if else
python list comprehension multiple variables
python lambda function list comprehension
python list comprehension two lists
python list comprehension filter

Is there a way of using the list-comprehension "syntax" in functions, to reduce calculation/calls (a bit like try/catch) e.g a way of writing


for customer in customers:
  if movie not in customers[customer]:


for customer in customers:
  p.append(movie) if movie not in p=customers[customer]

if the call/operation p was very heavy, thus it makes a big deal, if it is called once or twice

You can use the items method of a dict in a list comprehension:

[ expression for key,val in customers.items() ]

so you could abuse it to have the side-effect of appending:

[ val.append(movie) for key,val in customers.items() if movie not in val ]

The actual value of the list-comprehension is almost useless: it'll be a list of None (but its length will tell you how many val did not contain movie).

Sometimes, shorter is not better style. I think this may be one of them.

Edit: just realised key is unused, so the .values() method is better (I find a use for it so rarely I forgot about it!)

[ val.append(movie) for val in customers.values() if movie not in val ]

When to Use a List Comprehension in Python – Real Python, In the example above, the member value is i . iterable is a list, set, sequence, generator, or any other object that can return its elements one at a  A list comprehension is a syntactic construct available in some programming languages for creating a list based on existing lists. It follows the form of the mathematical set-builder notation (set comprehension) as distinct from the use of map and filter functions.

Not sure what kind of "calculation/calls" you are trying to reduce, but if it is the double lookup of customers[customer], you can do it like this:

for movies in customers.values():
    if movie not in movies:

.values() iterates over the values of the dict (so in your case, the list of movies for each customer), so it avoids iterating on the customers and looking up the list of movies twice. That works assuming customers is a dict of lists as values. You can even enhance that with a dict of sets:

for movies in customers.values():

List Comprehension in Python - Better Programming, , they consist of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element. The syntax of List Comprehension in Python For a python list comprehension, we use the delimiters for a list- square brackets. Inside those, we use a for-statement on an iterable (a list, here). We’ll take an example.

One-line code:
[movies.append(movie) for movies in customers.values() if movie not in movies]

dict.values() returns a list-like object of the values of a dictionary, excluding the keys. The line above evaluates a list of return values of list.append, but list.append returns None, and the list is discarded anyways.

What I recommend is to actually write out the full logic:

for movies in customers.values():
    if movie not in movies:

and to use list comprehension only when you actually need the list. It will only confuse those who don't understand list comprehension, and it does not provide any speedup or cleaner code.

Also, if you don't want repeating items in a list, consider a set, which contains distinct elements and provides a speedup.

Python List Comprehensions: Explained Visually, provide us with a short and concise way to construct new sequences (such as lists, set, dictionary etc.) using sequences which have been already defined. List comprehension is an elegant and concise way of creating a list in Python. There are many benefits of using Python List comprehensions and the most basic benefit can be seen from the syntax alone.

Comprehensions in Python, Learn how to effectively use list comprehension in Python to create lists, to replace (nested) for loops and the map(), filter() and reduce() functions, ! A list in Python is different from, for example, int or bool , in the sense that  Rewriting the list comprehension as a for loop provides us with the same output: Output. ['s', 'h', 'a', 'r', 'k'] List comprehensions can be rewritten as for loops, and some for loops can be rewritten to be list comprehensions to make code more succinct.

(Tutorial) Python List Comprehension, List comprehension is a complete substitute for the lambda function as well as the functions map(), filter() and reduce(). For most people the syntax of list  Which one's a better way of doing list comprehension in python (in terms of computation time & cpu cycles). In example (1) is the value f(r) evaluated in each iteration or is it evaluated once and cached ? y = [x*f(r) for x in xlist] c = f(r) y = [x*c for x in xlist] where. def f(r): some arbitrary function

Python Tutorial: List Comprehension, Now when we know the syntax of list comprehensions, let's show some Now, let's see how we can use list comprehension in functions. A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it. For example, this listcomp combines the elements of two lists if they are not equal:

  • p already exists as customer does it not? list comprehensions should be used when you want a list as an output, not to reduce lines of code
  • You are effectively trying to use a comprehension for its side effects (appending to existing lists), which is discussed here.
  • it is not about list-comprehension, but to reduce the call customers[customer] from two to one
  • Loop the values instead of the keys, also, you can get more performance by using a set for movies instead of a list
  • It is for each customer i want to check, if that movie is already in his list, i.e the call customers[customer] (list of movies) is called twice: one for checking if movie is in that list and once more when we append it.