+ 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