Printing List Elements on Separate Lines in Python

How can I format a list to print each element on a separate line in python?

You can just use a simple loop: -

>>> mylist = ['10', '12', '14']
>>> for elem in mylist:
print elem

10
12
14

Python Print list on separate lines

cursor.execute('SELECT username, score FROM Players order by score DESC limit 5')
topscore = cursor.fetchall()

topscore = list(topscore)
for i in topscore:
print(i[0],i[1],sep=' , ')
print('\n')

print list elements line by line - is it possible using format

You can use the string formatter on really any kind of string, including multi-line string. So of course, if you had a format string '{}\n{}\n{}' you could pass three items to it, and they would be all placed on separate lines.

So with a dynamic number of elements you want to print, all you need to do is make sure that the format string contains the same number of format items too. One way to solve this would be to construct the format string dynamically. For example this:

'\n'.join('{}' for _ in range(len(my_list))).format(*my_list)

So you essentially create a format string first, by having a generator produce one format item {} per element in my_list, and joining these using a newline character. So the resulting string looks something like this: {}\n{}\n…\n{}\n{}.

And then you use that string as the format string, and call format on it, passing the unpacked list as arguments to it. So you are correctly filling all spots of the format string.

So, you can do it. However, this is not really a practical idea. It looks rather confusing and does not convey your intention well. A better way would be to handle each item of your list separately and format it separately, and only then join them together:

'\n'.join('{}'.format(item) for item in my_list)

As for just printing elements line by line, of course, the more obvious way, that wouldn’t require you to build one long string with line breaks, would be to loop over the items and just print them one-by-one:

for item in my_list:
print(item)

# or use string formatting for the item here
print('{}'.format(item))

And of course, as thefourtheye suggested, if each loop iteration is very simple, you can also pass the whole list to the print function, and set sep='\n' to print the elements on separate lines each.

How can my function print out lines of # . The argument is a list of integers and they specify how many # each line should contain

Your list_of_char() func returns a list, which you then print out. So you're seeing exactly what you're asking for.

If you want to print out strings, you need to either create the string in list_of_char() and return the string instead of a list. Or, you can take the returned list and print the elements of that list one at a time. Or you can convert the list of strings into a single string and print that out.

In the func you could change:

return my_new_list

to:

return '\n'.join(my_new_list)

which then would have the func return a single string.

In the alternative, you could change "main" from:

print(list_of_char([2,3]))

to:

print('\n'.join(list_of_char([2,3])))

Or, you could handle the list items one at a time (which now means not adding the extra newline (because you'll get one by default from print():

for line in list_of_char([2,3]):
print(line)

Print list of lists in separate lines

Iterate through every sub-list in your original list and unpack it in the print call with *:

a = [[1, 3, 4], [2, 5, 7]]
for s in a:
print(*s)

The separation is by default set to ' ' so there's no need to explicitly provide it. This prints:

1 3 4
2 5 7

In your approach you were iterating for every element in every sub-list and printing that individually. By using print(*s) you unpack the list inside the print call, this essentially translates to:

print(1, 3, 4)  # for s = [1, 3, 4]
print(2, 5, 7) # for s = [2, 5, 7]

how can I split a list and print it in separate lines?

If you want to output the list new-line separated you can use #join and use the newline-character (\n) as separator for your list entries.

For example:

bot.send_message(chat_id = update.message.chat_id , text = "\n".join(list))

which will output:

username1
username2
username3

If you explicitly want the [ & ] outputed as well, you can use format to add them:

text = "[\n{}\n]".format("\n".join(list))
bot.send_message(chat_id = update.message.chat_id , text = text)


Related Topics



Leave a reply



Submit