adda.fuentes
adda.fuentes

Reputation: 365

Display list result horizontally in python 3 rather than vertically

I created a function in python to count the length of words while ignoring punctuation symbols. For example if I have the sentence : "Haven't there been 3 blackouts today? The result should be the following: [6,5,4,1,9,5] I am able to get these results with the following function I created:

import string
def word_length_list(given_string):
        clean_string = given_string.translate(str.maketrans('','',string.punctuation))
        word_lenght_list = list(map(len, clean_string.split()))
        return word_lenght_list
    given_string = "Haven't there been 3 blackouts today?"
    word_length_list(given_string)

This function gives me the expected result which is: [6, 5, 4, 1, 9, 5] However if I give it a really long string such as:

given_string = "Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function."

It gives me the result in the following way:

[7,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 8]

Does anyone have any ideas of how I could make my list return the result horizontally for both short string and long string? Any suggestions would be greatly appreciated.

I am using jupyter notebook to run the code as a .ipynb

Upvotes: 11

Views: 9679

Answers (3)

Gaurav Luitel
Gaurav Luitel

Reputation: 13

I guess the only way could be printing the variable word_lenght_list, instead of returning it.

Upvotes: 0

Tom Hale
Tom Hale

Reputation: 46775

Join the representation of the list's items with ', ',

l = [3.14, 'string', ('tuple', 'of', 'items')]
print(', '.join(map(repr, l)))

Output:

3.14, 'string', ('tuple', 'of', 'items')

Upvotes: 1

Realhermit
Realhermit

Reputation: 425

In a separate cell run the command %pprint

This turns pretty printing off. The list will now display horizontally.

Upvotes: 18

Related Questions