python - Why sorted list_b has no value? -
this question has answer here:
- sort() , reverse() functions not work 4 answers
- why “return list.sort()” return none, not list? 6 answers
i'm total beginner. please give me explanation why list_b
has no value simple code.
list_a = [3,2,1,4] list_b = list_a.sort() print(list_a,list_b)
i thought list_b has value of list_a.sort()
[3,2,1,4]
has none
value. please me understand this.
you can try use
list_b = sorted(list_a)
or
list_a = [3,2,1,4] print(list_a) list_a.sort() print(list_a)
sorted() builds new sorted list iterable, leaving original list unaffected.
list.sort() sorts list in-place, mutating list indices, modifies list in-place (and returns none avoid confusion). it's less convenient sorted() - if don't need original list, it's more efficient.
see more details how sort.
Comments
Post a Comment