Print 5 Items in a Row on Separate Lines for a List

Print 5 items in a row on separate lines for a list?

It needs to invoke for-loop and join functions can solve it.

l=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

for i in range(len(l)/5+1):
print "".join(l[i*5:(i+1)*5]) + "\n"

Demo:

abcde

fghij

klmno

pqrst

uvwxy

z

How to print X items in a list per line

You can do that as follows:

for i,item in enumerate(listA):
if (i+1)%4 == 0:
print(item)
else:
print(item,end=' ')

Printing multiple items in list in different lines

To have a list which goes from 1 to 25 you need to do range(1, 26). Apart from that, you can print it in the format you asked just by doing:

numbers = range(1, 26)
for i, elem in enumerate(numbers):
if i % 5 == 0:
print()
print(str(elem).ljust(3), end='')
print()

Output:

1  2  3  4  5  
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

enumerate(list) returns a pair (index, element) for every element in the list. str.ljust(n) left-justifies the string so that it is n characters long, padding the rest with spaces.

EDIT: alternatively, as proposed by @PM_2Ring in the comments:

numbers = range(1, 26)
for i, elem in enumerate(numbers, 1):
print(str(elem).ljust(3), end='\n' if i % 5 == 0 else '')

As an alternative to str(elem).ljust(3) you can also use '{:<3}'.format(elem) (Python 2.7+) or '{0:<3}'.format(elem) (Python <2.7)

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.

Printing an int list in a single line python3

You want to say

for i in array:
print(i, end=" ")

The syntax i in array iterates over each member of the list. So, array[i] was trying to access array[1], array[2], and array[3], but the last of these is out of bounds (array has indices 0, 1, and 2).

You can get the same effect with print(" ".join(map(str,array))).



Related Topics



Leave a reply



Submit