-2

I wrote a simple code to check if a list is sorted or not. I have two questions: First, my result is wrong. I guess the issue is with following the line. Which one is correct?:

sorted_list = mylist[:].sort()
sorted_list = list(mylist[:]).sort()

As second question, when I try to print(sorted_list), I get NameError which is sorted_list is not defined which is weird. Because I've already defined it in the second line. Would you please help me understand why I'm getting this error?

def is_sorted(mylist):
    sorted_list = mylist[:].sort()
    # mylist.sort()
    if sorted_list == mylist:
        return True
    else:
        return False


print(is_sorted(['Aajid', 'Bafiee', 'Hello']))
print(sorted_list)

Output:

False
Traceback (most recent call last):
  File "e:\NectCloud\Python\is_sorted.py", line 11, in <module>
    print(sorted_list)
          ^^^^^^^^^^^
NameError: name 'sorted_list' is not defined

Thanks

Majid
  • 421
  • 6
  • 19
  • 1
    `sorted_list` is a *local* variable, not a global variable. It goes out of scope as soon as `is_sorted` returns. – chepner Feb 01 '23 at 12:24
  • 1
    It doesn't matter how you define the variable. The problem is that it is defined inside of a function. So there is no `sorted_list` variable in the global scope – Yevhen Kuzmovych Feb 01 '23 at 12:24
  • 1
    Assign your list to a variable, use the *variable* as the argument, then print that variable. – chepner Feb 01 '23 at 12:25
  • 3
    (As an aside, the `sorted` function returns a new sorted list without modifying the original. `sorted_list = sorted(my_list)`. Also, `sort` returns `None`, not the sorted list.) – chepner Feb 01 '23 at 12:26
  • 2
    `def is_sorted(mylist): return mylist == sorted(mylist)` – chepner Feb 01 '23 at 12:26
  • 1
    To add to @chepner's comment, sorting is not the best (fastest) way to determine whether the list is sorted. I suggest checking [this question](https://stackoverflow.com/questions/3755136/pythonic-way-to-check-if-a-list-is-sorted-or-not). – Yevhen Kuzmovych Feb 01 '23 at 12:39

1 Answers1

0

Use

mylist = [1, 5, 3, 6, 2]
sorted_list = sorted(mylist)

mylist.sort() does inplace sorting and returns None. For more information about sorting see https://docs.python.org/3/howto/sorting.html.

sorted_list is not defined in outer scope since it is only defined in the function scope. See https://realpython.com/python-scope-legb-rule/.

Ken Jiiii
  • 474
  • 9
  • 21
  • @YevhenKuzmovych I am not sure what you mean. First question was how to properly sort a list. As `sort` returns `None` both options mentioned from the author are not valid. The second question was why there is a name error. And this error exists because the variable is defined in function scope and not in global scope. This has nothing to to with `is_sorted` anyway, has it? If you mean that `mylist` shoudn't be overridden `sorted` should be used. Is that what you are referring to? With a terneray it would be `is_sorted = True if sorted(mylist) == mylist else False`. – Ken Jiiii Feb 01 '23 at 12:56