0

.sort() is not working as I expect it to. I intended to create a Python program that sorts two copies of the same array in ascending and descending order.

Initial code

def MyFunction(Num):
    StrNum = str(Num)
    ArrNum = [letter for letter in str(Num)]
    print(ArrNum)

    print('Ascending and Descending Arrangements')
    AscendingNum = ArrNum
    DescendingNum = ArrNum

    DescendingNum.sort(reverse=True)
    print(DescendingNum)
    AscendingNum.sort()
    print(AscendingNum)


MyFunction(1032)

Result

DescendingNum = ['3', '2', '1', '0']

AscendingNum = ['0', '1', '2', '3']

I reprinted them.

Result with ascending sorted last

DescendingNum = ['0', '1', '2', '3']

AscendingNum = ['0', '1', '2', '3']

Result with descending sorted last

DescendingNum = ['3', '2', '1', '0']

AscendingNum = ['3', '2', '1', '0']

My guess at why it is happening

It appears that the second sorting is affecting the first. Although I have read many articles on .sort(), I cannot grasp why this is happening; after all, they are two separate arrays. Yet, I feel that the reason might lie in the following part.

    AscendingNum = ArrNum
    DescendingNum = ArrNum

What is the reason and solution?

Maadhav Bhatt
  • 69
  • 1
  • 9
  • `sort` changes your list in place and you are referring to the very same list in both cases. Either use `copy()` on the original list to generate a new one or use the `sorted()` function which will generate a new sorted list for you. – fsimonjetz Jun 11 '21 at 09:00
  • Because the issue is not with the sort. Both `AscendingNum` and `DescendingNum` point to same variable `ArrNum`, so change made to one reflects to the other. – Yoshikage Kira Jun 11 '21 at 09:00
  • Why was this question downvoted? The question is clear, and there is a reproducible example. If you think it's too simple of a question, remember that you too started from somewhere – ignoring_gravity Jun 11 '21 at 09:02
  • @ignoring_gravity Wasn't me but this question has been asked several times. – Yoshikage Kira Jun 11 '21 at 09:05
  • @ignoring_gravity It probably was downvoted for insufficient prior research, not because it's too simple. This issue of people not understanding they have one list with two names is a *very* common problem for people new to Python. – PM 2Ring Jun 11 '21 at 09:05
  • This may be helpful: [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html), by Ned Batchelder. Also, [Other languages have "variables", Python has "names"](https://web.archive.org/web/20180411011411/http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables). – PM 2Ring Jun 11 '21 at 09:09
  • BTW, we don't normally use CamelCase in Python for simple identifiers, we use it for class names. Please see the [PEP 8 -- Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/). – PM 2Ring Jun 11 '21 at 09:12

2 Answers2

2

Yet, I feel that the reason might lie in the following part.

AscendingNum = ArrNum DescendingNum = ArrNum

Indeed, if you also try to print AscendingNum is DescendingNum, you'll get True


If you want them to both be new objects, you could do

AscendingNum = list(ArrNum)
DescendingNum = list(ArrNum)
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
1

it's not working cause on the second example you work on the same list object

this happens cause:

AscendingNum = ArrNum
DescendingNum = ArrNum

i would recommand (or using deepcopy):

AscendingNum = ArrNum.copy()
DescendingNum = ArrNum.copy()
Stefan Schulz
  • 533
  • 3
  • 8