'Python: Printing a list with a line break after every nth item

I have a list of media that I want to print in the following format, with a line break after every nth item:

media_list = ['A001', 'A002', 'A003', 'A004', 'A005', 'A006', 'A007', 'A008']

A001 OR
A002 OR
A003 OR
A004 OR

A005 OR
A006 OR
A007 OR
A008 OR

But I'm not sure how to create the line break. This is what I have so far:
(I was thinking of using the gaps tuple in a list comprehension somehow...)

print('\n'.join('{}\tOR'.format(m) for m in media_list))
gaps = tuple(range(0, len(media_list), 4))[:0:-1]

In reality the list will include hundreds of entries and the line break will need to be after every 30th.
First python script, help appreciated!



Solution 1:[1]

Not sure it is what you want. Hope it helps :)

media_list = [
    'A00{}'.format(i) for i in range (1,10)
]

nth_item = 4

str_list = [
    '{}\tOR\n'.format(m)
    if(((media_list.index(m)+1) % nth_item) == 0)
    else
    '{}\tOR'.format(m)
    for m in media_list
]

print('\n'.join(str_list))

Solution 2:[2]

You can also use

for i, e in enumerate(my_list, n_elements):
    print(e)

Replace my_list with your list name and n_elements with the number of elements after which you want the line break.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 lu5er