assignment with [:]

Robert Kern robert.kern at
Sun Dec 28 06:12:27 CET 2008

Ben Bush wrote:
> Hi,
> I saw this line of code on a recent post:
> a1[:] = [x*3 for x in a1]
> Could somebody tells me what the [:] means? I can't find it anywhere.

It's a slice assignment. When both the start and stop arguments are omitted, it 
refers to the entire sequence. In this case, it means to replace the entire 
contents of the list a1 with the value on the right-hand-side.

"a[2:4] = ..." would replace the 3rd and 4th items in the list with the sequence 
on the right-hand-side, for instance.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

More information about the Python-list mailing list