Printing out a sorted list in Python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Printing out a sorted list in Python?

I have a list that is sorted by the length of strings in it. I want to print it out, without the brackets and commas. Or maybe even in columns like this: One Five Three Letter Two Four Seven Eleven If anyone has an idea I would be most grateful!

23rd Mar 2018, 1:23 PM
Višeslav Dasović
Višeslav Dasović - avatar
16 Answers
+ 14
i = 0 for word in words: print(word + " ") i += 1 if i == 4: print("\n)
23rd Mar 2018, 1:40 PM
Vukan
Vukan - avatar
+ 10
If you just want to print a list without the brackets and commas, you can use an asterisk to unpack the list (a.k.a the 'splat' operator.) e.g. print(*["one", "two", "three"]) # output -> one two three Applying * on any iterable object, by placing it to the left of the object, produces the individual elements of the iterable. If applied on a list-like iterable, it produces the elements of the list in the order they appear in the list. If applied on a dict-like object, it produces the keys of the dict in the order you would get as if you iterated the dict. (from https://codeyarns.com/2012/04/26/unpack-operator-in-JUMP_LINK__&&__python__&&__JUMP_LINK/)
25th Mar 2018, 5:30 AM
David Ashton
David Ashton - avatar
+ 6
@David a special output is wanted. Given 100 words. Sort them by len. Output as colums: all words with same length in one col. It is a nice challenge....
25th Mar 2018, 8:23 AM
Oma Falk
Oma Falk - avatar
23rd Mar 2018, 2:26 PM
Oma Falk
Oma Falk - avatar
23rd Mar 2018, 3:15 PM
Oma Falk
Oma Falk - avatar
+ 4
it was a nice brainteaser thanks to you, you are welcome!
23rd Mar 2018, 3:36 PM
Oma Falk
Oma Falk - avatar
+ 2
Thats perfect!!! Exactly what I was looking for! Thanks Oma 😁
23rd Mar 2018, 3:32 PM
Višeslav Dasović
Višeslav Dasović - avatar
+ 1
So what I am trying to do: enter 3-9 letters, check if they are letters and if there is 3-9 of the. Done Take these letters, mix ‘em up, and for every “mixture” check in dictionery if it is there. Done. Put every hit in a list. Done. And the print it out nicely. Not sure how to do the last part I’ll try sending a code so you see exectly.
23rd Mar 2018, 2:35 PM
Višeslav Dasović
Višeslav Dasović - avatar
+ 1
https://code.sololearn.com/cZ3Q4VvVUxtm/#py This is my code. Note that I am learning Python for one week so please don't laugh :P
23rd Mar 2018, 2:43 PM
Višeslav Dasović
Višeslav Dasović - avatar
+ 1
Dic is not defined. Sorry... I’m a noob. Also, how would I use regex in python. I want to say ‘start of line’ (with ^ caret) I know with grep it would be ‘^word’
23rd Mar 2018, 3:23 PM
Višeslav Dasović
Višeslav Dasović - avatar
+ 1
For example if I check if ‘gla’ is in dictionary, it would find it in glass and say ‘found it!!’ But gla is not a word. If I were using grep I would use regex like Grep ‘^gla\ ‘ dictionary
23rd Mar 2018, 3:27 PM
Višeslav Dasović
Višeslav Dasović - avatar
0
Thanks! Any idea how I would sort the output in columns?
23rd Mar 2018, 1:43 PM
Višeslav Dasović
Višeslav Dasović - avatar
0
Well it depends on the amount of words in the list. I made a program that finds all possible word out of the letter you give it. And I want the word to be outputted in columns depending on the amount of letters
23rd Mar 2018, 1:47 PM
Višeslav Dasović
Višeslav Dasović - avatar
0
So three letters in first column four in second etc...
23rd Mar 2018, 1:47 PM
Višeslav Dasović
Višeslav Dasović - avatar
0
Not sure how to do that :D
23rd Mar 2018, 1:50 PM
Višeslav Dasović
Višeslav Dasović - avatar
0
@vukan that makes every word from the list go to a new line.
23rd Mar 2018, 2:59 PM
Višeslav Dasović
Višeslav Dasović - avatar