cherukupally kalyan
cherukupally kalyan

Reputation: 11

why shallow copy is working different for nested list elements and non nested list elements?

Why shallow copy working in different for nested list elements and non nested list elements? I don't understand why it is doing like this.

It is doing shallow copy function is working in two different ways:

  1. for non-nested list elements working as deep copy
  2. for nested list elements working as shallow copy.

here I am providing the code for that. please give me clarity on this. with graphics possibly.

lst_1 = [1,2,[3,4],5]

lst_2 = lst_1.copy()

lst_1[2][0] = 'Now'

lst_2[0]='haha'

print(lst_1)

print(lst_2)

output is:

[1, 2, ['Now', 4], 5]

['haha', 2, ['Now', 4], 5]

Upvotes: 1

Views: 21

Answers (0)

Related Questions