+ 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!
16 Answers
+ 14
i = 0
for word in words:
print(word + " ")
i += 1
if i == 4:
print("\n)
+ 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/)
+ 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....
+ 5
@viseslav
might work:
https://code.sololearn.com/c3S02y62Mz50/?ref=app
+ 4
it was a nice brainteaser
thanks to you, you are welcome!
+ 2
Thats perfect!!! Exactly what I was looking for! Thanks Oma 😁
+ 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.
+ 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
+ 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’
+ 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
0
Thanks! Any idea how I would sort the output in columns?
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
0
So three letters in first column four in second etc...
0
Not sure how to do that :D
0
@vukan that makes every word from the list go to a new line.