0

I'm looking for an oneliner for this below logic:

links (list) variable will contain = ['**www.google.com/api/1.js**','**/path.js**']
url = "mysite.com"
valid_links = [url + u for u in links if not urlparse(u).netloc ]

This will give me only ['mysite.com/path.js'] but not 'www.google.com' in the list. I need full urls as it is and paths appended with my own url.

So i added else block:

url = "mysite.com"
valid_links = [url + u for u in links if not urlparse(u).netloc else u]

Can someone help me to get rid of the syntax error for this oneliner.

I tried it with

for #[iter]
   if:
      ....
      append to list
   else:
      ....
      append to list

But i want understand how to convert it in a oneliner

deceze
  • 510,633
  • 85
  • 743
  • 889
Sunil Raj
  • 43
  • 4
  • 2
    Why do you want a one liner? Using multiple lines is much more readable – mousetail Mar 25 '23 at 08:09
  • Does this answer your question? [Does Python have a ternary conditional operator?](https://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator) – mousetail Mar 25 '23 at 08:11
  • 3
    Complex one-liners are for programmers who have no empathy with their colleagues who may, one day, have to modify the code – DarkKnight Mar 25 '23 at 08:11
  • @DarkKnight. Valid point. But its good to know how a 6 line code can be done in a one line. – Sunil Raj Mar 26 '23 at 07:42

1 Answers1

0

I'm looking for an oneliner.

Here is it:

from urllib.parse import urlparse
links = ['**www.google.com/api/1.js**','**/path.js**']
url = "mysite.com"
valid_links = [ url+'/'+u for u in links if not urlparse(u).netloc]
print(valid_links)

and if you want to implement if/else the right syntax would be:

valid_links = [url+'/'+u if not urlparse(u).netloc else u for u in links]

both variants printing in case of the given list:

['mysite.com/**www.google.com/api/1.js**', 'mysite.com/**/path.js**']

Notice that:

The provided if/else expression works anywhere. I use it often in assignments like for example:

x = 2 if y==3 else 1

It sets x to 2 if the variable y has the value 3 and if not it sets x to 1.

The if in the list comprehension is an item selection filter and an else does not make any sense there.

In other words the if/else expression has nothing to do with the list comprehension as such. You don't want to skip any items from the list (this is the purpose of a final if or multiple ifs in list comprehension). You want the resulting item value to be another one depending on a condition.

See Ternary conditional operator in Python (Wikipedia)

Claudio
  • 7,474
  • 3
  • 18
  • 48
  • Thanks. So in this case, i if ... else ... {for [iter[]}. So the condition is about one item in the list . Perfect. Thanks. – Sunil Raj Mar 25 '23 at 09:45
  • This condition works anywhere. I use it often in assignments like for example: `x = 2 if y==3 else 1` (this sets x to 2 if the variable y has the value 3 and if not it sets x to 1). The 'if` in the list comprehension is an item selection filter and `else` does not make sense there. So notice that the given expression if ... else has nothing to do with the list comprehension as such. You don't skip any items from the list (this is the purpose of the final `if` in list comprehension). You want the resulting item value be another one depending on a condition. – Claudio Mar 25 '23 at 10:01
  • Yes. Instead of using if as a filter for the comprehension, i should be using it for the item itself. Thanks. – Sunil Raj Mar 26 '23 at 07:38