-1
a=[1,2,3,4,5]
b=a
a.sort()

what are the values of a and b now? I am getting both a and b are same. But how to make it different

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
naveen
  • 23
  • 1
  • 5
  • 2
    Does this answer your question? [How to clone or copy a list?](https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list) – Shubhzgang May 19 '20 at 04:38
  • Yes, *because `a` and `b` are two variables that refer to the **same** object*. You did that yourself when you did `b = a`, that's what that means. As an aside, please tag all python related questions with the generic [python] tag. – juanpa.arrivillaga May 19 '20 at 04:49

1 Answers1

1

One way is to initialize b=[] and append all elements of a to b, one by one.

    a=[1,2,3,5,4] #Let's say
    b=[]
    for i in a:
        b.append(i)
    a.sort()

Now a and b will be different.