Python Forum
Uses cases for list comprehension (encountered while doing PyBite #77) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Uses cases for list comprehension (encountered while doing PyBite #77) (/thread-29885.html)



Uses cases for list comprehension (encountered while doing PyBite #77) - Drone4four - Sep-24-2020

For as long as I have been studying Python, I encounter other forum members and all kinds of guides around the web who use list comprehension. It’s actually quite frequent. Apparently it's Pythonic. Many times when I see list comprehension used by other programmers online, I am compelled to rewrite them using a regular for loop because I find for loops to be ‘more readable’.

For example, take a look at this list comprehension demo in my REPL:

>>> S = [2*n for n in range(0,9) if ( (n % 2) == 0)]
>>>print(S)
[0, 4, 8, 12, 16]
While that technique is concise and many experienced Pythonistas would consider it “Pythonic”, I personally find it confusing and very, very hard to follow. I guess it comes easier with practice. Here is my attempt at re-writing that list comprehension line but using a regular for loop instead:

>>> def multiples_of_four(n):   
...    newlist = []
...    for n in range(0,n*2):
...        if n % 4 == 0:
...            newlist.append(n)
...    return newlist
... 
>>> multiples_of_four(9)
[0, 4, 8, 12, 16]
See? It achieves the same output and in my humble opinion in that situation, it’s more readable and easier to understand.

However this past weekend I was solving for PyBite #77 and I came across a terrific answer on Stack Overflow. User sashkello shares a comprehension technique which compares the contents of two lists and creates a new list which includes items that are unique to each of the original lists. To achieve that task, taken from his answer, on SO sahskello writes:

outputlist = [a for a in list1+list2 if (a not in list1) or (a not in list2)]
Now compare that technique to the same output using a regular for loop:

def xor(list1, list2):
    outputlist = []
    list3 = list1 + list2
    for i in range(0, len(list3)):
        if ((list3[i] not in list1) or (list3[i] not in list2)) and (list3[i] not in outputlist):
             outputlist[len(outputlist):] = [list3[i]]
    return outputlist 
The list comprehension technique in this particular use case is far easier to understand in my opinion. Wouldn’t you people agree?

Can anyone else come up with any other high-impact outstanding use cases for list comprehension? What other examples could you people demonstrate where list comprehension is far superior than a regular for loop?


RE: Uses cases for list comprehension (encountered while doing PyBite #77) - buran - Sep-24-2020

So, your first example of list comprehension is better as
S = [2*n for n in range(0,9) if not n % 2]
and this makes it even more readable
Then, the implementation with "regular" for loop would be:
def multiples_of_four(n):   
    newlist = []
    for i in range(0, n):
        if not i % 2:
            newlist.append(i * 2)
    return newlist

print(multiples_of_four(9))
note the difference with your implementation. at the moment both implementations are identical


Then comes your second example. I would not be so exited with SO solution. IMHO list comprehension is better as

list1 = [1, 2, 3]
list2 = [2, 3 ,4]

output_list = [a for a in list1+list2 if a in set(list1) ^ set(list2)]
print(output_list)
not to mention that if order does not matter:
print(list(set(list1) ^ set(list2)))
now your implementation is way more complicated than necessary:
def symetric_difference(list1, list2):
    new_list = []
    for a in list1 + list2:
        if a not in list1 or a not in list2:
            new_list.append(a)
    return new_list

print(symetric_difference(list1, list2))



RE: Uses cases for list comprehension (encountered while doing PyBite #77) - perfringo - Sep-25-2020

(Sep-24-2020, 05:25 PM)Drone4four Wrote: While that technique is concise and many experienced Pythonistas would consider it “Pythonic”, I personally find it confusing and very, very hard to follow.

It usually coming from not understanding the syntax of comprehension. I have converted a few 'non-believers' into fans by just showing diagram like below and pointing out that you should always try to spell your idea in spoken language and only after that translate it into Python

Output:
where you get it from what you │ on which conditions get │ you get it ╭─┴─╮╭───────┴─────────╮ ╭────┴──────╮ [2*n for n in range(0,9) if not n % 2] ╰─┬─╯╰───────┬─────────╯ ╰────┬──────╯ give me │ if item multiple │ is even of item │ for every item in range
Isin't 'give me multiple of item for every item in range if item is even' nicely and concisely put in spoken language :-)


RE: Uses cases for list comprehension (encountered while doing PyBite #77) - ndc85430 - Sep-25-2020

Arguably, if you learnt the list comprehension first it might have made sense. Kevlin Henney in one of his talks, argues exactly that, arguing in favour of programming declaratively: https://www.youtube.com/watch?v=NSzsYWckGd4. I think he's a great speaker.