g | x | w | all
Bytes Lang Time Link
043Tcl211130T004637Zsergiol
011Dyalog APL241205T192347ZAaron
006Uiua241205T142401Znyxbird
037AWK241202T191133Zxrs
039Clojure240923T080104ZMartin P
082Turing Machine240922T184128Zz..
025Arturo221111T144911Zchunes
034BASH240912T150855Zjohn
040C clang240912T140248Zjdt
012Qdeql240911T224348ZBbrk24
nanPxem rpxem v0.0.7240910T131939Z鳴神裁四点一号
013Labyrinth240318T064223ZBubbler
048Python 3240222T023041ZLarry Ba
007Uiua SBCS231105T170326Zchunes
063Rockstar230622T114654ZShaggy
002Vyxal231107T162349Zpacman25
017TIBasic211110T231300ZYouserna
131Whitespace231105T194108ZKamila S
039Python 3.8 prerelease230623T153935ZHunaphu
036Prolog230623T140844Zcoredump
007Pip xp230306T164101ZBaby_Boy
nan230306T170455ZThe Thon
037rSNBATWPL221111T152613Zrydwolf
035Knight220807T004956ZnaffetS
039ECMAScript 2015 with Lodash Array220719T202418ZEric Xue
038Desmos220717T071143ZMathEnth
005Brachylog220720T112650ZFatalize
010sed n220717T133241ZJonah
nan220717T061337ZCzylabso
008Perl 5 + p220715T104053ZDom Hast
036Nibbles220714T165134ZDominic
4010MMIX220714T224551ZNoLonger
015APL220714T155625ZVadim Tu
087x8616 machine code211113T143308Z640KB
004Husk220104T234557ZNatte
029Factor + sequences.merged211110T173917Zchunes
009BQN211209T155018Zovs
038Desmos211111T065330ZAiden Ch
027Pari/GP211111T023457Zalephalp
022Wolfram Language Mathematica211110T183630Zatt
067CLCINTERCAL211113T154143Zuser1004
050C++ gcc211115T170143Zjdt
006Burlesque211118T140537ZDeathInc
011Vim211118T215952ZAaroneou
047Rust v1.37.0211110T181753ZMayube
032TSQL211112T135303Zt-clause
048Coconut211113T192818ZAlexandr
041AWK211112T235715ZPedro Ma
nanPxem211113T155137Zuser1004
0224211112T225231ZConor O&
007convey211110T174553Zxash
003><>211110T191251Zemanresu
039Python 2211111T153627ZElPedro
037Red211111T080710ZGalen Iv
009J211112T075140ZGalen Iv
007Ly211111T014339Zcnamejj
065Whitespace211111T134404ZKevin Cr
010K oK211111T135304Zmkst
nanPure Bash211111T104203Zuser1004
026Ruby211111T074959ZG B
047Icon211111T082810ZGalen Iv
00405AB1E211111T080714ZKevin Cr
030Python 2211111T074738Zxnor
029R211111T021654ZGiuseppe
115Core Maude211111T013255ZChris Bo
031Python 3 + numpy211111T001703Zloopy wa
009brainfuck211111T001108Zdingledo
004Japt211111T000157ZAZTECCO
022Haskell211110T233716ZAZTECCO
017Raku211110T172929ZSean
010CJam211110T231652ZLuis Men
035Python 3.8 prerelease211110T231627Zloopy wa
098Rockstar211110T210121ZMayube
010Add++211110T191014Zcaird co
031PowerShell211110T190348Zuser3141
007Pip rp211110T190006ZDLosc
042C clang211110T171340ZNoodle9
057Add++211110T170625ZFmbalbue
023JavaScript ES6211110T175413Zrydwolf
005Charcoal211110T175246ZNeil
002Vyxal r211110T174819ZAaroneou
026C# .NET Core with Linq211110T173425ZMayube
018Zsh211110T173623Zpxeger
006ayr211110T173119ZZippyMag
017APL+WIN211110T172958ZGraham
034Python 3211110T171359Zm90
007Pyth211110T171300Zwasif
011Grok211110T165736ZAaroneou
036Python 3211110T164738Zhyperneu
021Perl 5 p211110T164640ZKjetil S
004Vyxal211110T164255ZAaroneou
004Jelly211110T164111Zhyperneu

Tcl, 43 bytes

proc S L {join [lmap a\ b $L {list $b $a}]}

Try it online!


# [Tcl], 47 bytes
proc S L {lmap a\ b $L {lappend M $b $a}
set M}

Try it online!

Dyalog APL, 11 chars

,⌽⍤⊢⌺(⍪2 2)

A function, à la:

    f←,⌽⍤⊢⌺(⍪2 2)
    f 1 2 3 4 5 6
2 1 4 3 6 5

Uiua, 6 bytes

♭⧈⇌¤¤2

Try it!

 ⧈ ¤¤2 # for each pair of elements
  ⇌    # reverse them
♭       # flatten

AWK, 37 bytes

{for(;i++<NF;i++)printf$(i+1)FS$i" "}

Attempt This Online!

Bonus AWK/Regex 42 bytes

$0=gensub(/([^,]+),([^,]+)/,"\\2,\\1","g")

Attempt This Online!

Clojure, 39 bytes

#(mapcat(fn[[a b]][b a])(partition 2%))

Examples:

(#(mapcat(fn[[a b]][b a])(partition 2%)) [1,2,3,4,5,6])
=> (2 1 4 3 6 5)

(#(mapcat(fn[[a b]][b a])(partition 2%)) [0,1,0,1])
=> (1 0 1 0)

Turing Machine, 82 bytes

a * * b _ *^ > <
b * * c _ *^ > >
c * * c * * > >
c * _ a * *^ < >
c _ * d * * - -

enter image description here

Arturo, 37 30 25 bytes

$->x->@[loop x[a,b][b,a]]

Try it

BASH, 34 bytes

for((;$#;)){ printf $2$1;shift 2;}

Try it Online!

Input is the first argument.

C (clang), 40 bytes

f(*l,n){*l=*l<<16|*l>>16;n&&f(l+1,n-1);}

Try it online!

The function treats pairs of short values (each 16 bits) as a single 32-bit int and swaps their halves.

Qdeql, 12 bytes

Two different twelve-byte solutions:

&&\**\/\/&&/
&\&=\/\/**&/

Both take input as a sequence of bytes from stdin, write the output in the same way to stdout, and terminate on EOF or upon encountering a null terminator (since those are impossible to tell apart in Qdeql).

Example:

$ ./run_qdeql.sh '&\&=\/\/**&/' 'abcdef'
badcfe

Pxem (rpxem v0.0.7), 0 + 15 = 15 bytes

Golfed off 3 bytes. Expect list of code points.


Filename

.w.p.i.i.c[\x01].+.a

Try it online!

Labyrinth, 13 bytes

??!
; \
,\!
@

Try it online!

Takes a list of integers separated by any non-numeric non-null single byte, and prints newline-separated integers. It is important that the input does NOT have a trailing separator, since it is used to detect the end of input.

Loop: ??!\!\,;
??    Take two integers A and B [A B]
!\!\  Print B, newline, A, newline
,     Take the next separator, -1 on EOF
;     Continue loop if not EOF; discard the separator

End: @ (Halt)

Python 3, 48 bytes

f=lambda l:l[::-1][:2]+f(l[2:])if len(l)>1else l

Try it online!

Very simple. For some reason l[:2:-1] doesn't work.

Uiua SBCS, 7 bytes

♭≡⇌↯∞_2

Try it!

-1 thanks to Bubbler

♭≡⇌↯∞_2
   ↯∞_2  # reshape into matrix with two columns
 ≡⇌      # reverse columns
♭        # deshape

Rockstar, 63 bytes

Takes input as a space separated string, outputs each element on its own line. 9 bytes could be saved if we were guaranteed every element of the array was a single digit by taking input with no delimiting spaces.

listen to S
cut S with " "
while S
roll S in N
say roll S
say N

Try it (Code will need to be pasted in)

listen to S        :Read the input string into variable S
cut S with " "     :Split S to an array on spaces
while S            :While S is not empty
roll S in N        :  Pop the first element from S into variable N
say roll S         :  Pop the first element from S and output it
say N              :  Output N

Vyxal, 16 bitsv2, 2 bytes

2ẇR

Try it Online!

Bitstring:

0100011100000110

another 2 byter

TI-Basic, 19 17 bytes

seq(Ans(I-i²^I),I,1,dim(Ans

i represents the imaginary number.

Takes input in Ans. Output is stored in Ans and is displayed.

-2 bytes thanks to MarcMush.

Whitespace, 131 bytes

   	 
	
		   	 
			   	 
	 	 
  	
 
 
	 	 
   
	
		   	
	
		   	
				
 	   	     
	
     
				
 	   	     
	
     	
	  	
 
	

  	 

Attempt This Online!

Generated from the following asm2ws program:

2 / GETN / 2 / RCL / 2 / DIV
@LOOP
    DUP / BZ %END
    0 / GETN
    1 / GETN
    1 / RCL / PUTN
    32 / PUTC
    0 / RCL / PUTN
    32 /PUTC
    1 / SUB / B %LOOP
@END

Python 3.8 (pre-release), 39 bytes

f=lambda x:x and[x.pop(~len(x)%2)]+f(x)

Try it online!

Prolog - 36 bytes

s([],[]).s([A,B|L],[B,A|R]):-s(L,R).

Non code-golf:

;; swap empty list is empty list
swap([],[]).

;; swap of A,B followed by L is B,A followed by R, where swap(L,R).
swap([A,B|L],[B,A|R]):-swap(L,R).

Pip -xp, 5 8 7 bytes

R_MF<>a

Try It Online!

Thunno, \$ 6 \log_{256}(96) \approx \$ 4.94 bytes

ZlrzZS

Attempt This Online!

Explanation

ZlrzZS  # Implicit input     [1, 2, 3, 4, 5, 6]
Zl      # Uninterleave       [[1, 3, 5], [2, 4, 6]]
  r     # Reverse            [[2, 4, 6], [1, 3, 5]]
   zZ   # Zip                [[2, 1], [4, 3], [6, 5]]
     S  # Sum (Flatten)      [2, 1, 4, 3, 6, 5]

rSNBATWPL, 37 bytes

a~(a cond (a{1},0 a)+{a sfro 2}){+for

This is very golfed. It is "bad rSNBATWPL" in that it abuses the syntax very heavily. The ungolfed version of this is:

f = a ~ cond{a} {
    (a{1}, a{0}) conc f{a sfro 2}
} { cast.array{""} }

This is a simple recursive lambda function, which iterates pair-by-pair. Some golfs I made to this were:

Knight, 35 bytes

;=aP;=bP WaIb;Ob;Oa;=bN=aP Ia=bP=aP

Try it online!

ECMAScript 2015 with Lodash (Array), 39 bytes

l=>_.chunk(l,2).flatMap(s=>[s[1],s[0]])

Solution below works if array doesn't chunk evenly (l.length % 2 == 1)

l=>_.chunk(l,2).flatMap(s=>s.length-1?[s[1],s[0]]:s)

Desmos, 42 38 bytes

-4 bytes thanks to @aiden's suggestion and wackscope variables

l=[1...L.length]
f(L)=L[l-1+2mod(l,2)]

Try It On Desmos!

Brachylog, 5 bytes

ġ₂↔ᵐc

Try it online!

Explanation

ġ₂         Split the list in groups of 2 consecutive elements
  ↔ᵐ       Map reverse
    c      Concatenate

sed -n, 10 bytes

2~2{G;p};h

Try it online!

Julia

25 bytes

!x=x[1:end.|>i->i-(-1)^i]

Attempt This Online!

24 bytes

!x=x[(I=1:end).-(-1).^I]

Perl 5 + -p, 8 bytes

This sets $_ (implicitly printed by -p) to the next line of STDIN (<> - which isn't trimmed via chomp) concatenated with the current line ($_).

$_=<>.$_

Try it online!

or (with -nl -M5.10.0):

say<>.$_

Try it online!


Perl 5 + -p -M5.10.0, 8 bytes

This outputs (say) the next line of STDIN (<>), after converting to a number ($-=) to avoid the trailing newline, then the current line is output via -p.

say$-=<>

Try it online!

Nibbles, 3.5 3 bytes (6 nibbles)

\+\`/~
   `/      # chunks of input
     ~     # (using default size = 2),
  \        # reverse,
 +         # concatenate,
\          # and reverse again.

enter image description here

MMIX, 40 bytes (10 instrs)

Assumes 8-byte size for list elements.

void __mmixware swao(uint64_t *l, size_t len)

hexdump (jxd -T):

00000000: 42010009 3f010101 27010101 8fff0000  B¢¡µ?¢¢¢'¢¢¢Ɓ”¡¡
00000010: 8f020008 afff0008 af020000 e7000010  Ɓ£¡®Ḥ”¡®Ḥ£¡¡ḃ¡¡Ñ
00000020: 5b01fffa f8000000                    [¢”«ẏ¡¡¡

Disassembled:

swae    BZ   $1,1F          // if nothing, return
        SRU  $1,$1,1        // turn length into loop count
0H      SUBU $1,$1,1        // decrement loop count
        LDOU $255,$0,0      // load l[0]
        LDOU $2,$0,8        // load l[1]
        STOU $255,$0,8      // store the other way
        STOU $2,$0,0        // store the other way
        INCL $0,16          // l += 2
        PBNZ $1,0B          // if loop count isn't yet 0, loop back
1H      POP  0,0            // return

APL, 15 bytes

{,⌽⍵⍴⍨(2÷⍨≢⍵)2}

The same idea as in the existing solution, but implemented as a separate function and two bytes less.

x86-16 machine code, 8 7 bytes

00000000: aca4 aa49 e2fa c3                        ...I...

Listing:

    TWO_LOOP: 
AC      LODSB               ; load first byte (into AL) 
A4      MOVSB               ; move second byte into first byte 
AA      STOSB               ; store first byte (from AL) into second byte 
49      DEC  CX             ; only loop half as many times as length 
E2 F9   LOOP TWO_LOOP 
C3      RET

Callable function: input list at [SI], output to [DI] length in CX.

-1 bytes thx to @m90!

Husk, 4 bytes

ṁ↔C2

Try it online!

Explanation

ṁ↔C2
  C2  slices of length 2
ṁ↔    flatmap reverse

Factor + sequences.merged, 34 29 bytes

[ 2 group unzip swap 2merge ]

Try it online!

Explanation

        ! { 1 2 3 4 5 6 }
2 group ! { { 1 2 } { 3 4 } { 5 6 } }
unzip   ! { 1 3 5 } { 2 4 6 }
swap    ! { 2 4 6 } { 1 3 5 }
2merge  ! { 2 1 4 3 6 5 }

BQN, 10 9 bytesSBCS

-1 byte thanks to Razetime!

⥊·⌽˘∘‿2⊸⥊

Run online!

∘‿2⥊𝕩 Reshape the input into a matrix with 2 columns and and the necessary number of rows.
⌽˘ Reverse horizontally (Swap the 2 columns).
Flatten into a vector.

An alternative using the Under operator at 10 bytes:

⌽˘⌾(∘‿2⊸⥊)

Try it online!

Desmos, 39 38 bytes

l=[1...L.length]
f(L)=L[l-1+2mod(l,2)]

Try It On Desmos!

Try It On Desmos! - Prettified

Pari/GP, 27 bytes

l->[l[i-(-1)^i]|i<-[1..#l]]

Try it online!

Wolfram Language (Mathematica), 22 bytes

0<##<1||#2&&#&&#0@##3&

Try it online!

Input [L...], and returns in an And. Works on integer inputs.


Wolfram Language (Mathematica), 24 bytes

{}=={##}||#2&&#&&#0@##3&

Try it online!

Input [L...], and returns in an And. Works on non-boolean inputs.

Wolfram Language (Mathematica), 26 bytes

#2~##&~#&@@@#~Partition~2&

Try it online!

Input [L], and returns a list. Works.

CLC-INTERCAL, 95 67 bytes.

Why does CLC-INTERCAL 1.-94.-2 lack !1~.1'? I could have golfed off one byte.

DOCOMEFROM#9(1)DOWRITEIN.1+.2DDOCOMEFROM'.1~.1'~#1(9)DOREADOUT.2+.1

Copy and paste to try it online!

Usage

Try these inputs

ONE
TWO
ONE
TWO
ZERO
ONE
TWO
THREE
FOUR
FIVE
SIX
ZERO

C++ (gcc), 50 bytes

[](auto&v){for(int&i:v)swap(i,(&i)[int(&i)%8<1]);}

-5 bytes thanks to ceilingcat!

Try it online!

Burlesque, 6 bytes

2co)^p

Try it online!

Improved: Remembered map shorthand

2co   # Chunks of 2
)^p   # Push in reverse for each block

Burlesque, 7 bytes

2co{}^m

Try it online!

2co   # Chunks of 2
{}^m  # Push in reverse for each block (strictly push-map with a nop)

Vim, 11 bytes

qqddpj@qq@q

Try it online!

Explanation:

qq    @qq@q # Loop:
  dd        #  Delete the line
    p       #  Paste it below the next line
     j      #  Move down a line

Rust v1.37.0, 59 55 47 bytes

|a:&[_]|(0..).zip(a).map(|i|a[i.0^1]).collect()

Try it online!

-2 bytes thanks to ZippyMagician
-2 bytes thanks to alephalpha
-8 bytes thanks to (0..).zip(a)

Rust is not a golfy language...
Port of the C# and JS answers
v1.37.0 is the version available on TIO

Rust stable, 54 bytes

|a:&[_]|a.chunks(2).flat_map(|a|[a[1],a[0]]).collect()

Try it online!

Thanks to ZippyMagician for this slightly golfier answer on the latest version of Rust.

T-SQL, 32 bytes

Input is a table variable

SELECT a FROM @ ORDER BY b+b%2*2

Try it online

Coconut, 48 bytes

l->(l[1::2],l[:-1:2])|*>zip|>reduce$(+)

Try it online!

Python equivalent:

from functools import reduce
from operator import add
f = lambda l: reduce(add, zip(l[1::2], l[:-1:2]))

AWK, 43 41 bytes

{for(;++n<NF;n++)$n+=$(n+1)-($(n+1)=$n)}1

Thanks for Dominic van Essen for 2 less bytes!

Try it online!

{
for(;++n<NF;n++)        Starts a loop though the numbers.
                        n has to start as 1, because $0 is the whole line.
$n+=$(n+1)-($(n+1)=$n)  Swap the numbers two by two.
                        Readable: a += b - (b = a)
}
1                       Prints the line.

Pxem, 18 + 0 = 18 bytes.

The configure is:

Filename:

.w._._.n .o.n .o.a

Content is empty.

Try it online!

Usage

4, 22 bytes

3.72082072152152072094

Try it online!

Readable + explanation:

3. 720 820 721 521 520 720 9 4
^^------------------------------ mandatory prefix
   ^^^-------------------------- initial input in cell 20
       ^^^-----------------^---- loop while cell 20 is nonzero
           ^^^------------------ input to cell 21
               ^^^-------------- output cell 21
                   ^^^---------- output cell 20
                       ^^^------ input to cell 20
                           ^---- end loop
                             ^-- exit

Corresponding Quartic program:

decl a, b
input a
loop a
  input b
  print b
  print a
  input a
end

convey, 7 bytes

I knew I should have implemented SpaceChem's flipflop operator, then this would be 5 bytes. :-)

-6 byte by @Manny Queen

}?{
}~1

Try it online!

{ puts the input list on the conveyor, } prints the result. On the ? the elements try to go down if they can. On the ~ they will wait for 1 tick before going to the output, thus starting with the first item, every second element is delayed for two ticks.

13 bytes

},<
{@^
0"
1^

Try it online!

The lower loop 0"\1^ copies 0 1 0 1 … into choose @. The input is thus split into two paths. The right one takes a little bit longer, so the other number can overtake it before joining , again.

variant 2 with input 1 to 6

><>, 4 3 bytes

i#o

Try it online!

Lol, ><> ties with beats Jelly & 05AB1E. Terminates with an error.

-1 thanks to @Manny Queen.

How does it work?

     The instruction pointer (IP) is currently moving right
i    Take input as a character
 #   Reflect - The IP starts moving left
i    Take input again
  o  Output that
 #   Reflect - The IP starts moving right again
  o  Output the first input we took - the inputs are now swapped

And now, we're moving left and back at the start again on i + moving right. 
This loops forever (i.e. until erroring when we're out of input) 
because there is no halt instruction (;)
This is essentially executing "iioo" in an infinite loop.

Python 2, 49 39 bytes

lambda l:[l[c^1]for c in range(len(l))]

Try it online!

-10 with thanks to @MannyQueen for an awesome tip.

Simply because no-one has submitted a Python 2 answer yet.

Red, 37 bytes

func[a][foreach[b c]a[print[c lf b]]]

Try it online!

Alternative

Much longer but more fun approach using parse:

Red, 59 bytes

func[a][parse a[collect[any[b: skip keep skip keep(b/1)]]]]

Try it online!

J, 9 bytes

[:,_2|.\]

Try it online!

Ly, 7 bytes

ir[foo]

Try it online!

Pretty straightforward, but might be worth posting since it's almost pronounceable. :)

ir       -- read input codepoints into the stack, reverse the order
  [   ]  -- while the stack isn't empty
   foo   -- flip the top two entries and print as characters

Cubix, 14 bytes

..@.i?i.o;o;.^

Try it online!

This is the first time I've tried to use Cubix, so I wouldn't be suprised if it's possible to do this with less code. It's a hard language to explain, since it's a 2D language where the code is wrapped around a cube. So the directions the instruction pointer take a hard to show in a description. I'll include a link to the online interpreter since it has a debug mode when you can see the way the code iterates through the cells.

But I'll try to explain it too...

..@.i?i.o;o;.^ - code before it's wrapped on the cube...
    i          - (1) starts here, input a codepoint
     ?         - branch if top of stack, <0 "left", >0 "right"
  @            - "left" (true on EOF), halt program
             ^ - "right", set IP direction to "up"
     ?         - same branch, but coming from another direction
      i        - "right", input another codepoint
        o;     - output top of stack as char, pop it off stack
          o;   - repeat... code wraps back to (1)

The . characters are no-ops and are needed to place the other characters in the right position so they will wrap into place on the cube.

Here's a link to the online interpreter http://ethproductions.github.io/cubix/?code=Li5ALmk/aS5vO287Ll4=&input=MTIzNDU2Nzg=&speed=20

Whitespace, 65 bytes

[N
S S N
_Create_Label_LOOP][S S S N
_Push_0][S N
S _Duplicate_0][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input1][S N
S _Duplicate_input1][S N
S _Duplicate_input1][T  N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input2][T N
S T _Print_input2_as_integer][S S S T   S T S N
_Push_10][S N
S _Duplicate_10][T  N
S S _Print_10_as_character][S N
T   _Swap_top_two][T    N
S T _Print_input1_as_integer][T N
S S _Print_10_as_character][N
S N
N
_Jump_to_Label_LOOP]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Whitespace doesn't have lists, so I/O are newline delimited (strictly non-negative) integers.

Try it online (with raw spaces, tabs and new-lines only).

Explanation in pseudo-code:

LOOP:
  Integer a = STDIN as integer
  Integer b = STDIN as integer
  Print b as integer to STDOUT
  Print '\n' as character to STDOUT
  Print a as integer to STDOUT
  Print '\n' as character to STDOUT
  Go to next iteration of LOOP

This will stop with an error automatically when there are no more inputs.

: For every second input it will use the input before it as heap-address ([SNS_Duplicate_input1][SNS_Duplicate_input1][TNTT_Read_STDIN_as_integer]), so the inputs should be strictly non-negative, since negative heap addresses aren't allowed in Whitespace.

K (oK), 10 bytes

Solution:

,/|:'0N 2#

Try it online!

Explanation:

,/|:'0N 2# / the solution
     0N 2# / reshape into n x 2 grid
  |:'      / reverse (|:) each (')
,/         / flatten

Alternatives:

Pure Bash, 32 + 1 = 33 bytes

One additional byte for filename specification: x.

echo "$2
$1"
shift 2
(($#))&&. x

Try it online!

Usage


Pure Bash, 34 + 1 = 35 bytes

One additional byte for filename specification: x.

read x
read y||>x
echo "$y
$x"
. x

Try it online!

Usage

Ruby, 26 bytes

->l{r=-1;l.map{l[1^r+=1]}}

Try it online!

Icon, 47 bytes

procedure f(L)
L[i:=1to*L by 2]:=:L[i+1]&\z
end

Try it online!

05AB1E, 4 bytes

2ôí˜

Try it online or verify all test cases.

4 bytes alternatives:

ιRø˜

Try it online or verify all test cases.

2ô€`

Try it online or verify all test cases.

Explanation:

2ô   # Split the (implicit) input-list into parts of size 2
     #  i.e. [1,2,3,4,5,6] → [[1,2],[3,4],[5,6]]
  í  # Reverse each pair
     #  → [[2,1],[4,3],[6,5]]
   ˜ # Flatten the list of pairs
     #  → [2,1,4,3,6,5]
     # (after which this list is output implicitly as result)

ι    # Uninterleave the (implicit) input-list, using 2 as implicit second argument
     #  i.e. [1,2,3,4,5,6] → [[1,3,5],[2,4,6]]
 R   # Reverse it
     #  → [[2,4,6],[1,3,5]]
  ø  # Zip/transpose, swapping rows/columns
     #  → [[2,1],[4,3],[6,5]]
   ˜ # Flatten the list of pairs
     #  → [2,1,4,3,6,5]
     # (after which this list is output implicitly as result)

2ô   # Split the (implicit) input-list into parts of size 2
     #  i.e. [1,2,3,4,5,6] → [[1,2],[3,4],[5,6]]
  €  # Map over each pair:
   ` #  Pop and push the pair to the stack
     # (€` is usually used to flatten a multi-level list one level down. One side-effect
     # is that the items are dumped in reversed order, which is what we want in this
     # case)
     #  → [2,1,4,3,6,5]
     # (after which this list is output implicitly as result)

Python 2, 30 bytes

def f(a,b,*m):print b,a,;f(*m)

Try it online!

The function f takes input splatted like f(1,2,3,4,5,6), and prints the output space-separated, terminating with error.


34 bytes

lambda l:map(l.pop,len(l)/2*[1,0])

Try it online!

This works by alternating popping the elements at index 1 and index 0.

R, 29 bytes

function(l)l[seq(!l)+2*1:0-1]

Try it online!

Core Maude, 115 bytes

mod S is pr LIST{Nat}. op f : Nat ~> Nat . var A B : Nat . eq f(A B X:[Nat])=
B A f(X:[Nat]). eq f(nil)= nil . endm

The result is obtained by reducing the f function with the input list.

Example Session

Maude> red f(1 2 3 4 5 6) .  --- Expected: 2 1 4 3 6 5
result NeList{Nat}: 2 1 4 3 6 5
Maude> red f(0 1 0 1) .  --- Expected: 1 0 1 0
result NeList{Nat}: 1 0 1 0

Ungolfed

mod S is
    pr LIST{Nat} .

    op f : Nat ~> Nat .

    var A B : Nat .

    eq f(A B X:[Nat]) = B A f(X:[Nat]) .
    eq f(nil) = nil .
endm

Just the straightforward recursive solution. Saved a couple bytes by inlining the type in X:[Nat] rather than declaring it with the var keyword. Doing the same with A and B doesn't change the byte count — just the way the math works out.

Python 3 + numpy, 31 bytes

lambda m:m[~-(m==m).cumsum()^1]

Try it online!

Expects a numpy array. Most of the code is about avoiding referencing numpy directly, so we can avoid the explicit import. It then calculates the indices directly by xoring a counter with 1.

brainfuck, 9 bytes

,[>,.<.,]

Try it online!

Japt, 4 bytes

ò mé

Try it

ò      - chunks of 2
  mé   - rotate each

Haskell, 22 bytes

f(a:b:c)=b:a:f c
f x=x

Try it online!

Raku, 19 17 bytes

*.flatmap:{$^b,$^a}

*[^* »+»<1 -1>]

Try it online!

CJam, 10 bytes

N{riripp}h

Try it online!

Explanation

N           e# Push newline (this is truthy)
 {      }h  e# Do while without popping the condition: infinite loop
  r         e# Read line and push string to the stack
   i        e# Interpret as integer
    r       e# Read line and push string to the stack
     i      e# Interpret as integer
      p     e# Pop and print with a newline
       p    e# Pop and print with a newline
            e# Implicitly print stack: newline

Python 3.8 (pre-release), 35 bytes

def f(m):*m[1:],_,m[::2]=*m,m[1::2]

Try it online!

In-place. Essentially a golfed version of m[1::2],m[::2]=m[::2],m[1::2]

Rockstar, 98 bytes

Listen to B
cut B with ","
I is 0
while I is less than B
shout B at I+1
shout B at I
Build I up up

Try it online!
(Must manually copy-paste code)

Takes input as comma-separated string of numbers eg:

1,2,3,4,5,6

Outputs as newline-separated string of numbers eg:

2
1
4
3
6
5

Rockstar has no modulo, and no bitwise operators, so porting the other solutions is out of the question.

Explanation

Listen to B             ; Read input to B
cut B with ","          ; Split B on ","
I is 0                  ; Set I to 0
while I is less than B  ; While I < B.length
                        ; (Rockstar Arrays automatically coerce to length when used as a scalar)
shout B at I+1          ; Print B[I+1] with trailing newline
shout B at I            ; Print B[I] with trailing newline
Build I up up           ; Increment I twice (each 'up' increments)

Add++, 10 bytes

L,2$T€bRbF

Try it online!

Uses Add++'s much more powerful functional mode, rather than it's vanilla mode. However, definitely check out the other Add++ answer, as it doesn't use any form of indexing, just looping!

How it works

L,2$T€bRbF - Unnamed lambda function. Takes 1 argument, L
L,         - Define the lambda:
  2$T      - Split into runs of length 2
     €     - Over each:
      bR   -   Reverse it
        bF - Flatten and return

PowerShell, 31 bytes

%{If(++$i%2){$j=$_}Else{$_,$j}}

Input comes from the pipeline.

Try it online!

Try it in a PS console:

$array = 1,2,3,4,5,6
$result = $array |
%{If(++$i%2){$j=$_}Else{$_,$j}}
"$($array -join ',') --> $($result -join ',')"

Explanation

% is an Alias for the Cmdlet "ForEach-Object", which accepts input from the pipeline and processes each incoming object inside the ScriptBlock {...}
If(++$i%2) first increments $i (which starts at 0) in each loop and takes it modulo 2
If the result is 1:
{$j=$_} the current element will be saved in in $j
Otherwise (0):
{$_,$j} first puts the current element, then the element from the last loop iteration into the pipeline/stdout.

Pip -rp, 7 bytes

WVRVUWg

Try it online!

Explanation

The flags are for input & output formatting: -r reads the program arguments from lines of stdin; -p formats the output as a list rather than concatenating it.

      g  List of arguments
    UW   Unweave into a pair of lists, each consisting of every other element
  RV     Reverse the order of those two lists
WV       Weave them back together

C (clang), 53 \$\cdots\$ 43 42 bytes

f(*l,n){*l^=l[1]^(l[1]=*l);n&&f(l+2,n-2);}

Try it online!

Inputs a pointer in an int array and the array's length (since pointers in C carry no length info).
Swaps every two elements in place.

Add++, 57 bytes

a:?
b:0
n:0
m:3
`b
Ea,Ib=n,,b:1,,x:i,+1,Ib=m,,b:0,,Oi,,Ox

Try it online!

Input is the first argument.

JavaScript (ES6), 23 bytes

x=>x.map((_,i)=>x[i^1])

The [i^1] is thanks to m90 in TNB

Try it online!

Charcoal, 5 bytes

WS⟦Sι

Try it online! Link is to verbose version of code. Takes newline-terminated input. Explanation:

WS

Repeat while there is still input...

⟦Sι

... output the subsequent input before it.

Vyxal r, 2 bytes

yY

Try it Online!

Explanation:

y  # Uninterleave
 Y # Interleave

C# (.NET Core) with Linq, 32 26 bytes

a=>a.Select((n,i)=>a[i^1])

Try it online!

-8 bytes thanks to m90's witchcraft

C# (.NET Core), 73 bytes

a=>{for(int i=0,d=0;i<a.Length;i++){d=a[i];a[i]=a[++i];a[i]=d;}return a;}

Try it online!

Zsh, 18 bytes

for a b;<<<$b<<<$a

Attempt This Online!

ayr, 6 bytes

,_2,`/

Explained

 _2 … /    pairwise reduce
    ,`     By reverse concatenation
,         Ravel

APL+WIN, 17 bytes

Prompts for vector

,⌽((.5×⍴r),2)⍴r←⎕

Try it online! Thanks to Dyalog Classic

Python 3, 34 bytes

f=lambda l:l and l[1::-1]+f(l[2:])

Try it online!

When l is empty (which makes it falsy), return it directly. Otherwise, reverse the first two elements and make a recursive call for the rest.

Pyth, 7 bytes

.n_McQ2

Try it online!

Grok, 11 bytes

:Y}:zpz
  q

Try it Online!

Explanation:

:Y      # Get input and copy it to the register
  }     # Quit if no more input
  q
   :z   # Print next input
     pz # Print value in the register

Python 3, 36 bytes

lambda a:sum(zip(a[1::2],a[::2]),())

Try it online!

Perl 5 -p, 21 bytes

s/(\S+) (\S+)/$2 $1/g

Try it online!

Vyxal, 4 bytes

2ẇRf

Try it Online!

Explanation:

2ẇ   # Split list into groups of two
  R  # Reverse each group
   f # Flatten

Jelly, 4 bytes

s2UF

Try It Online!

s2    Slices of length 2
  U   Reverse each
   F  Flatten