python - Why sorted list_b has no value? -


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

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -