1
n1=['a','b','c']

n2=[1,2,3]  

n3=sorted(n1)+sorted(n2)  

n4=sorted(n1).extend(sorted(n2)) 

The problem is n3 is working fine but n4 is not. Why can't extend be after sorted()?

xlm
  • 6,854
  • 14
  • 53
  • 55
nanyin2017
  • 19
  • 2

3 Answers3

1

Though sorted() returns a sorted list, the problem is that extend() mutates the list and returns None, so you can achieve what you want by:

n4 = sorted(n1)
n4.extend(sorted(n2))
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
0

You aren't using extend correctly the command

n1.extend (n2)

returns a list called n1 which is ['a' , 'b' , 'c' , 1 , 2 , 3 ].

This is because you are extending a list.

Tank
  • 501
  • 3
  • 19
0

extend returns None.

print 'n2.extend([4]): ', n2.extend([4])
>>>> n2.extend([4]):  None
print 'n2', n2
>>>> n2 [1, 2, 3, 4]
Peter G
  • 2,773
  • 3
  • 25
  • 35
Lawes
  • 14