0

See in this python code, i want to retain the list k so i initialized the list to l and make changes in l.
But when i use the remove() function on "l" list changes are made in "k" also .
Can anyone tell why it is happening and any alternative? Is it something with remove function or i have done anything wrong in my code?
Thanks in advance.

k=[1,1,1,1,1]
l=k
comb=[1,1]
for i in comb:
   l.remove(i)
print(k) 
[output]:[1,1]

req output=[1,1,1,1,1]

kanav anand
  • 411
  • 1
  • 4
  • 14
  • 2
    See [this](https://jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither/) for a decent overall explanation. `l=k` means that both `l` and `k` are exactly the same object with two names. – roganjosh Aug 30 '17 at 16:02
  • 1
    k=[1,1,1,1,1] l=list(k) comb=[1,1] for i in comb: l.remove(i) print(k) – Dharmesh Fumakiya Aug 30 '17 at 16:13

0 Answers0