andriy andriy - 1 year ago 112
Python Question

What does [:] mean?

I'm analyzing some Python code and I don't know what

pop = population[:]

means. Is it something like array lists in Java or like a bi-dimensional array?
Could appreciate some help, thanks.

Answer Source

It's a slicing, and what it does depends on the type of population. If population is a list, this line will create a copy of the list. For an object of type tuple or a str, it will do nothing (the line will do the same without [:]), and for a (say) NumPy array, it will create a new view to the same data.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download