Print List of Lists in Separate Lines

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 to print a list of lists in different lines?

To answer your question, we must inspect the unpacking operator (*) and how it works with print.

Recall that the unpacking operator takes a single iterable and turns it into multiple elements which are passed in series to a function.

Then, we know that print takes multiple arguments in the form of args, using a separator to separate them, if necessary. With this in mind, we can see that the four ways to print a b c are equivalent:

print('a', 'b', 'c')
print(*['a', 'b', 'c'])
print(sep.join(['a', 'b', 'c']))
print('a' + sep + 'b' + sep + 'c')

...where sep is, by default, (a single space).

Now, we note that m is a nested list. Therefore, by doing print(*m), what you are doing is effectively print(m[0], m[1]...m[-1]), where m[0], m[1]...m-1 are individually lists.

By referring to our analysis of print above, we can see that that is the same as print(m[0] + ' ' + m[1] + ' ' + ... + ' ' + m[-1]).

Clearly, there are no newlines in that, but there should be newlines where there are now spaces.

To get what we want, which is a newline after each element, therefore, we can just do print(*m, sep='\n').

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

Print a nested list line by line - Python

Use a simple for loop and " ".join() mapping each int in the nested list to a str with map().

Example:

>>> ys = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
>>> for xs in ys:
... print(" ".join(map(str, xs)))
...
1 2 3
4 5 6
7 8 9 10

The difference here is that we can support arbitrary lengths of inner lists.


The reason your example did not work as expected is because your inner loop is iterating over each element of the sub-list;

for r in A:  # r = [1, 2, 3]
for t in r: # t = 1 (on first iteration)
print(t,)
print

And print() by default prints new-line characters at the end unless you use: print(end="") I believe if you were using Python 2.x print t, would work. For example:

>>> ys = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
>>> for xs in ys:
... for x in xs:
... print x,
... print
...
1 2 3
4 5 6
7 8 9 10

But print(x,) would not work as you intended it; Python 2.x or 3.x

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)

How to split a list of lists dynamically with next line separator

You can join the elements per row using ', ' as separator, then join the rows with a newline and print:

list_of_lists = [['England', '90.0%'], ['Scotland', '10.0%']]

print('\n'.join(map(', '.join, list_of_lists)))

output:

England, 90.0%
Scotland, 10.0%

Python - Output Elements in Two Lists on Separate Lines

Given:

>>> li1=['Chapter One Test', 'Chapter Two Test', 'Chapter Three Test']
>>> li2=['84%', '75%', '90%']

You can zip the two lists together:

>>> print('\n'.join(['{}\t{}'.format(*t) for t in zip(li1,li2)]))
Chapter One Test 84%
Chapter Two Test 75%
Chapter Three Test 90%

If you want to make a table, you might make the field widths fixed rather than \t separated:

>>> print('\n'.join(['{:22s}{:3s}'.format(*t) for t in zip(li1,li2)]))
Chapter One Test 84%
Chapter Two Test 75%
Chapter Three Test 90%

Read more about format mini-language to find out more about those options.



Related Topics



Leave a reply



Submit