-3

I started with something like this:

[a,b,s,d]
[k,e,f,s,d]
[o,w,g]

Then I wanted to rearrange them by length in descending order so that I get this:

[k,e,f,s,d]
[a,b,s,d]
[o,w,g]

However, to do that, I appended each of those into an array as such:

arr = [[a,b,s,d], [k,e,f,s,d], [o,w,g]] 

so that I could just use:

sorted(arr, key=len).reverse()

But now I can't unpack arr to just get:

[k,e,f,s,d]
[a,b,s,d]
[o,w,g]

Ideas?

Thanks.

Student J
  • 183
  • 1
  • 3
  • 13

1 Answers1

0

reverse() is an in-place function:

arr = [['a','b','s','d'], ['k','e','f','s','d'], ['o','w','g']] 
a = sorted(arr, key=len)
print a
# [['o', 'w', 'g'], ['a', 'b', 's', 'd'], ['k', 'e', 'f', 's', 'd']]
print a.reverse()
# None
print a
# [['k', 'e', 'f', 's', 'd'], ['a', 'b', 's', 'd'], ['o', 'w', 'g']]

reverse() has no output, but it does reverse the array.

njzk2
  • 37,030
  • 6
  • 63
  • 102