Output words from a list and sets in python

  1. How do I get words from a list and a set in Python without parentheses? For example, from the list ['end','nend'], you need to output [] without these brackets, so that there are just 2 words:

    end nend
    

    And from the set, the same thing.

  2. How do I output data in multiple rows?

Author: insolor, 2017-08-11

1 answers

You can connect the words in a single line using join with a space as a separator, and then output:

words = ['end', 'nend']
print(' '.join(words))

Another option, if you just need to output words, then you can pass the list of words to print through the "asterisk", then each word will be passed to print as a separate parameter:

words = ['end', 'nend']
print(*words)

To output the same words on several lines, in the first version, we simply change the separator to '\n':

words = ['end', 'nend']
print('\n'.join(words))

In the second option, you still need to explicitly set separator:

words = ['end', 'nend']
print(*words, sep='\n')

The output of the set is absolutely the same, i.e. we change the first line in all the examples to words = {'end', 'nend'}, and everything works, only for the set the word order can change (for the set, it is not guaranteed that the order of the elements will coincide with the original one).

 4
Author: insolor, 2017-08-11 11:32:35