r/Python • • Jun 23 '14

Writing better python code

https://gist.github.com/kracekumar/09a60ec75a4de19b346e
143 Upvotes

126 comments sorted by

View all comments

7

u/oconnor663 Jun 23 '14 edited Jun 23 '14

Is foo[:] really the Pythonic way to copy a list?

Edit: In Python 3, lists have their own .copy() method.

4

u/velit Jun 23 '14

I prefer newList = list(old_list)

Communicates intent better

6

u/poo_22 Jun 23 '14

Copy is very literal already, I argue that semantically it does not get better.

1

u/velit Jun 23 '14

Copying is a detail of slicing syntax, it's not the purpose of it. With the list constructor it's immediately evident there's a new instance involved - that's what constructors do.

foo[:] also looks weird.

4

u/poo_22 Jun 23 '14

I was talking about the list().copy method.