MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/Python/comments/28utww/writing_better_python_code/cievv6h/?context=3
r/Python • u/kracekumar • Jun 23 '14
126 comments sorted by
View all comments
7
Is foo[:] really the Pythonic way to copy a list?
foo[:]
Edit: In Python 3, lists have their own .copy() method.
.copy()
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.
4
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.
6
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.
1
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.
I was talking about the list().copy method.
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.