-1

I've been trying to find resources on how to read one-liner codes in Python, but most of the articles, StackOverFlow questions show already completed one-liners and most of them have little to no explanation of how it's constructed and what is the equivalent of the one-liner as a multi-line code.

Also the examples that I've seen that does explain a little bit are very simple examples.

But it's harder for me to read an example like the following:

Example:

var = [x for x in ip_networks if x in visited or (visited.add(x) or False)]

How do you read this line? And how is does it look as a multi-line code?

Fadi
  • 1,329
  • 7
  • 22
  • 40
  • 1
    This is a list comprehension. If you search for that term, you should find lots of resources. – Carcigenicate Sep 17 '21 at 19:54
  • 1
    Also this is code using a disfavored practice - assuming `visited` is a set or something similar! You shouldn't use code with side effects in a list comprehension predicate. – Peter DeGlopper Sep 17 '21 at 19:57
  • 1
    That line also likely doesn't do what you expect. I would assume you only want to add `x` to the result if it *isn't* in `visited` yet, and `visited.add(x)` *always* returns `None`, whether a new value was added to `visited` or not. – chepner Sep 17 '21 at 20:01
  • 1
    @PeterDeGlopper, can you elaborate on what aspect is disfavored? – NGilbert Sep 17 '21 at 20:11
  • @PeterDeGlopper, correct the actual code looks like this: https://techiedelight.com/compiler/?fiwz – Fadi Sep 18 '21 at 00:35
  • 1
    @NGilbert - people reading Python code expect list comprehensions to be calculations of the contents of a new list. They don't generally expect them to do anything other than build the new list. The terms of the comprehension are declaring how to transform the input and whether or not to include an element. It's usually bad style to use them for other things and have to keep track of anything other than what's in the new list. And here it doesn't even work, or at least it's not at all obvious why it's calling `.add()` and the final "or False" is definitely pointless. – Peter DeGlopper Sep 18 '21 at 04:28

1 Answers1

0

The syntax of list comprehension : [expression for item in iterable if condition == True] . Lets examine the original code which basically form a new list from nums containing duplicate elements:

nums = [1, 5, 2, 1, 4, 5, 1]
visited = set()
dup = [x for x in nums if x in visited or (visited.add(x) or False)]
print(dup)  # [1, 5, 1]

visited = the set we use to keep elements we see while iterating over the nums array
dup = the array we form with the elements that was seen before while iterating from start to end

  • While evaluating a condition like True or any_statement , any_statement is ignored.
  • visited.add(element) returns None no matter what, therefore "or False" statement is meaningless
  • or visited.add(x) is used to add the element to the visited set if it was not visited before, keep in mind that this statement is evaluated only if the first condition is not True
  • If you only wanted to keep [1,5] instead of [1,5,1], you could use a set instead of an array

Multiline version of the list comprehension part:

dup=[]
for x in nums:
   if x in visited:
      dup.append(x)
   else:
      visited.add(x)

Unsel
  • 343
  • 2
  • 8
  • Can you elaborate on how you've came up with this? I am trying to learn how to read this type of one liner, or if you know of a good resource for that that'd be great! – Fadi Sep 17 '21 at 22:07
  • Sure, the syntax of list comprehension is as follows: [expression for item in iterable if condition == True] . In your case your expression=x, item=x, iterable=visited, condition= 'x in visited or (visited.add(x) or False)'. You can also use an expression like x*x or anything you like to add squares of x to the list. You can check the link for a simple explanation. https://www.w3schools.com/python/python_lists_comprehension.asp – Unsel Sep 17 '21 at 22:26
  • I know I'm asking for too much, but is it possible to put it as part of the answer, this will be easier to read and also better for anyone else that might have the same issue. Also what is the `or False` in this case do? – Fadi Sep 18 '21 at 00:31
  • Like the `if x in visited or (visited.add(x) or False)` looks like it's also a one-liner? – Fadi Sep 18 '21 at 00:40