semantics of [:]

Esmail ebonak at hotmail.com
Fri Nov 20 17:15:55 CET 2009


Could someone help confirm/clarify the semantics of the [:] operator
in Python?

a = range(51,55)

############# 1 ##################
b = a[:] # b receives a copy of a, but they are independent


# The following two are equivalent
############# 2 ##################
c = []
c = a[:] # c receives a copy of a, but they are independent


############# 3 ##################
d = []
d[:] = a # d receives a copy of a, but they are independent


### 1 ### is the preferred, shorter way to do what ## 2 ## and ## 3 ##
do.

Am I correct with this?

Thanks.




More information about the Python-list mailing list