I am trying to copy a list and add it to an existing list of lists. Say that my original list is a = [1, 2, 3, 4, 5]
and I would like to append a copy of a
called b
to a list of lists called dataset
. To ensure that b
is a copy of a
, a[0] is b[0]
must be False. How can I achieve this without the module copy, using the list function and a loop of some sort?
Asked
Active
Viewed 49 times
1

Jake
- 12,713
- 18
- 66
- 96

user3530509
- 11
- 1
-
1Why can't you use the copy module? (One guess - this is homework? and if so, I think should be tagged as such?) – mfsiega Apr 14 '14 at 03:43
-
@mfrankli [The homework tag is now officially deprecated](http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-officially-deprecated). But it's a good idea to mention in the post that this is homework. – Christian Tapia Apr 14 '14 at 03:44
-
3I doubt that you can __guarantee__ `a[0] is b[0]` is `False` even with `copy.deepcopy` because implementations can do funny things to optimize immutable objects... – mgilson Apr 14 '14 at 03:44
-
To underscore @mgilson 's comment - check out [string interning](http://stackoverflow.com/questions/15541404/python-string-interning). – C.B. Apr 14 '14 at 03:49
1 Answers
3
Here's a hint which should point you in the right direction:
If a
is a list, then a[:]
returns a new copy of that list which is distinct from a
.
>>> a = [1, 2]
>>> b = a[:]
>>> a is b
False
>>> a == b
True

jaynp
- 3,275
- 4
- 30
- 43