Questions tagged [one-liner]

One Liners are an abridged one-line version of a multi-line script.

Originally, a one-liner program was textual input to the command-line of an operating system shell that performs some function in just one line of input. The one-liner can be

  • an expression written in the language of the shell;
  • the invocation of an interpreter together with program source for the interpreter to run;
  • the invocation of a compiler together with source to compile and instructions for executing the compiled program.

Certain dynamic scripting languages such as AWK, sed, and Perl have traditionally been adept at expressing one-liners. Shell interpreters such as Unix shells or Windows PowerShell allow for the construction of powerful one-liners.

The use of the phrase one-liner has been widened to also include program-source for any language that does something useful in one line.

Find more information at:

153 questions
-1
votes
1 answer

OneLiner explanation - Python

I have a task in LeetCode called 832. Flipping an Image Given an n x n binary matrix image, flip the image horizontally, then invert it, and return the resulting image. To flip an image horizontally means that each row of the image is reversed. For…
KacperG
  • 7
  • 3
-1
votes
1 answer

Python Ternary Operations

I have the following code: from django_app.models import Model def func_name(): name = "name" if Model.objects.filter(name=name).count() > 1: raise ValidationError("This name already exists.") else: return name Now I…
Saman
  • 29
  • 1
  • 8
-1
votes
1 answer

How to just remove 0x0a but not 0x0d 0x0a?

I want to remove the NL character but not the NL character after the CR character. tr would not do this. What is the best command to achieve this?
user1424739
  • 11,937
  • 17
  • 63
  • 152
-1
votes
1 answer

Is it possible to write multiple statements in one line that includes a for loop and if-else statements

I've been trying out some string manipulation and I'm trying to condense this into one line: y = 0 resultswords = [] for words in newstring: if words.lower() not in '_': resultswords.append(words) else: …
-2
votes
1 answer

Add item into list with one line if statement

I have the following code: a = True x = [2, 7, 8] print(x) if a: x.append(10) print(x) # one line ??? y = [2, 7, 8 if a: 10] print(y) I'm trying to use if statement in one line, but I get invalid syntax. Any ideas?
Joel
  • 1,805
  • 1
  • 22
  • 22
-2
votes
1 answer

Please explain return with "+1:" in it

Here is the code. 2 functions. def get_domains(self): #returns test@test.com, test2@test.com etc in json. if self.domain_names == None: r = requests.get(GET_DOMAINS_URL) if r.status_code != 200: raise…
Bogdan Mind
  • 65
  • 1
  • 1
  • 8
-2
votes
2 answers

Bash one liner for calculating the average of a specific row of numbers in bash

I just began learning bash. Trying to figure out how to convert a two-liner into a one liner using bash. The First Line of code... searches the first column of the input.txt for the word - KEYWORD. captures every number in this KEYWORD row from…
-2
votes
2 answers

Python One Line For Loop With If: Please Explain?

I am new-ish to python and one thing that always confuses me is when code is smashed into 1 line. Take the following for example: def how_sum(targert_sum, numbers): table = [None] * (targert_sum + 1) table[0] = [] for i in…
Nick
  • 35
  • 4
-2
votes
1 answer

Revoking blob URL after Creating it in a single line JavaScript

I am simply curious about how to do this differently. Right now I have created a single line of code that has NO semicolons or new lines. I want to make it clear that this has no errors at all whatsoever. The code creates an HTML blob made of…
myjobistobehappy
  • 736
  • 5
  • 16
-2
votes
3 answers

One line coding with if statement inside for loop

I need help shortening the following code into one line. for i in objects: if i not in uniq: uniq.append(i) Im just doing it for a challenge, not going to keep this.
Dav_Did
  • 188
  • 10
-3
votes
1 answer

importing module inside of a list comprehension in python

The below program will show loading in console. Can we make it one line? For that to happen we need to import time module inside of list comprehension. How can we import module inside of list comprehension? import time [print(f"\rLoading... " +…
Udesh
  • 2,415
  • 2
  • 22
  • 32
-3
votes
2 answers

why am i getting this?? at 0x000001C468108DC8>

i was trying to convert some part of my code into a one-liner but I am getting something unexpected print(x for x in [2,3,4,5] if x%2==0) can anyone tell why am I getting this - at 0x000001C468108DC8> instead of 2 and…
-3
votes
3 answers

How do I make this python code a one liner?

for _ in line: if line == "t": t += 1 else: s += 1 How do I make this python code a one liner?
-3
votes
1 answer

How can I commit everything that's changed in my git repo with just one command?

I want to commit all of the changes in my repository with one command. I know I can do it with two—using git add -A and then git commit -a—but, as a wise man once said... why waste time say lot word when few word do trick?. Is there not an -A option…
Brad Turek
  • 2,472
  • 3
  • 30
  • 56
-4
votes
3 answers

if else one liner in python

a=0 b=0 s="1110000" for i in range(len(s)): if s[i]=='1': a+=1 else: b+=1 this if else I need to write in one line. I tried below way it is not working. a+=1 if if s[i]=='1' else b+=1 PLease help me on this
raviteja.d
  • 11
  • 2
1 2 3
10
11