3

e.g. if tableState is a tuple, and we're declaring a variable:

x = tableState[1][:]

What would the [:] mean?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Adrienne
  • 2,540
  • 1
  • 29
  • 39

3 Answers3

5

It takes a copy of the tuple, instead of a reference to the tuple itself.

The [:] syntax is a slice with the default start and end values (0 and the length of the list), returning a new tuple with those indices, so effectively an exact copy of the contents.

For tuples, this doesn't make much sense, because tuples are immutable. You can safely pass around references to a tuple without changing what it's indices refer to.

It works the same for lists, though. You often need to create a copy of a list to be able to modify the contents, and using the default 'all elements' slice is a nice, concise and fast method of doing just that.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

In general, you're extracting a copy of the tuple. Keep in mind though that tuples are immutable, so taking a copy of a tuple doesn't really do you much good - it's far more useful with lists.

Makoto
  • 104,088
  • 27
  • 192
  • 230
0

[:] it is similer to [0:] which means split from 0 index to the end. it returns a new List .

Arpit
  • 12,767
  • 3
  • 27
  • 40