Python Forum
Uses cases for list comprehension (encountered while doing PyBite #77)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Uses cases for list comprehension (encountered while doing PyBite #77)
#1
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?
Reply
#2
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))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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 :-)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 506 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 470 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 1,246 Apr-02-2023, 06:31 PM
Last Post: tester_V
  list comprehension 3lnyn0 4 1,395 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  List comprehension used differently coder_sw99 3 1,710 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,824 Jul-17-2021, 04:30 PM
Last Post: maiya
  List comprehension and Lambda cametan 2 2,235 Jun-08-2021, 08:29 AM
Last Post: cametan
  What is the difference between a generator and a list comprehension? Pedroski55 2 2,212 Jan-02-2021, 04:24 AM
Last Post: Pedroski55
  For Loop with List Comprehension muzikman 25 6,574 Dec-18-2020, 10:45 PM
Last Post: muzikman
  Sorting list of names using a lambda (PyBite #5) Drone4four 2 2,725 Oct-16-2020, 07:30 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020