Morvolis
Morvolis

Reputation: 99

How to sort a multi-line list to a single-line list in python

How would you take an output from the concatenation of two lists and put it into one list. For example-

example1 = ["hello", "banana", "skit"]   
example2 = ["rodeo", "pine", "sky", "feel"]   
joined_list = example1 + example2

But the outcome is like this-

[ "hello" ,   
  "banana" ,   
  "skit" ,   
  "rodeo" ,   
  "pine" ,   
  "sky" ,   
  "feel" ]

I need it to be in a one line list, so the output should look like-

["hello", "banana", "skit", "rodeo", "pine", "sky", "feel"]

If someone also knows how to sort this list based off highest string length to lowest, so it ends up looking like-

["banana", "hello", "rodeo", "feel", "pine", "skit", "sky"]

Any help into properly formatting this would be much appreciated!

Upvotes: 0

Views: 313

Answers (1)

sahasrara62
sahasrara62

Reputation: 11238

use sort, on joined_list

example1 = ["hello", "banana", "skit"]   
example2 = ["rodeo", "pine", "sky", "feel"]   
joined_list = example1 + example2
joined_list.sort(key=len, reverse=True)
print(joined_list)

output

["banana", "hello", "rodeo", "feel", "pine", "skit", "sky"]

Upvotes: 2

Related Questions