Ordering a dictionary in Python [duplicate] - python

This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 2 years ago.
I do not understand why this code does not return the list ordered:
A = {2:'a', 1: 'b', 3:'c'}
R = list(a.keys()).sort()
In fact it does not return anything. I know I could do it in other ways like sorted(a.keys).
Thanks

sort() does in-place changes to the list. What you can do is create a variable to store the keys and then sort it.
R = list(a.keys())
R.sort()
Use R = sorted(list(a.keys())) instead, as sort() serve as an in-place function

Related

In colab, reverse a list then return a NoneType [duplicate]

This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 5 months ago.
I have a list M, containing 3 numpy arrays.
when I wrote M = M.reverse(), then M became the noneType.
Why this happens? To me the result should be a reversed list.
Thanks
Lists belong to the collection data types in Python and are implemented as objects with a set of collection methods, where not all methods return a value.
Specifically .reverse() is a method that directly modifies the list, but does not return a value, therefore to access the reversed list, you just use the variable it is assigned to (M in your case).

Issue in Python lists [duplicate]

This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 3 years ago.
Here is my code:
list_b=[1,2,3,4,5,6,7,8,9,10]
print (list_b)
if(1 in list_b):
t=list_b.append('hello')
print(t)
else:
t1=list_b.append(100)
print(t1)
In the console it is showing me None. (Image)
append() method does not return any value but updates existing list.
if you want to see the updated list, use print(list_b).
print(t) or print(t1) will return None as they don't have any return values.
You haven't stated the issue, but I'm assuming it's this:
t=list_b.append('hello')
append() modifies the array, it doesn't return a new array.

Different behavior using .extend and concatenation in Python lists [duplicate]

This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 7 years ago.
I read in this tutorial that you could add two lists using either + or the .extend() method. Other than performance issues, they produce the same results.
In that case, if I want to return the first and last four items of a list, using slicing, why does the .extend() return None while the + operator returns the correct result for the following code:
# return first and last four items in list
def first_and_last_4(itr):
first_four = itr[:4]
print(first_four)
last_four = itr[-4:]
print(last_four)
# return first_four.extend(last_four)
return first_four + last_four
my_list = list(range(1,50))
print(first_and_last_4(my_list))
list.extend modifies the existing list. It does not return anything. list + list returns a new list.
In your example, after calling first_four.extend(last_four), you should return first_four.

dict value can't be assigned while appending..? [duplicate]

This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 7 years ago.
If valst is a True list (containing elements), why does this work:
valst.append(seq)
id_seq_dict[id] = valst
But this does not work:
id_seq_dict[id] = valst.append(seq)
Is it because the append method returns nothing?
The append() method modifies the list and returns nothing (None) indeed.

Functions and variable assignment Python [duplicate]

This question already has answers here:
Why do these list operations (methods: clear / extend / reverse / append / sort / remove) return None, rather than the resulting list?
(6 answers)
Closed 8 years ago.
a=[8,2,15,6,1]
a = a.sort()
print a
Why does it print None? Can you elaborate on all functions?
sort() and sorted() are different:
sort() sorts the list in-place and returns None.
sorted() creates a new list and returns it.
See here for further details.

Categories