user7091717
user7091717

Reputation: 67

How can I include a new line everytime I append to a list?

The task is to create a 5x5 grid of "O"s inside one list. This is what I've written so far:

board = ["O"]

def lalala(lst):
    new_list = []
    for item in lst:
        new_list.append([item]*5)
    return new_list

print lalala(board)*5

and this is what it looks like now:

[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]

How do I make it look like a grid, something like this (but everything still has to be in one list):

[['O', 'O', 'O', 'O', 'O'], 
 ['O', 'O', 'O', 'O', 'O'], 
 ['O', 'O', 'O', 'O', 'O'], 
 ['O', 'O', 'O', 'O', 'O'], 
 ['O', 'O', 'O', 'O', 'O']]

and NOT like this (codecademy doesn't accept this):

[['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O']]
[['O', 'O', 'O', 'O', 'O']]

Codecademy suggests I use range() but I don't see how that will help.

Upvotes: 2

Views: 127

Answers (4)

Anthon
Anthon

Reputation: 76922

Because of this line:

new_list = []

your final statement is trying to print an object of type list. How an object is printed is defined by how it is converted to string. This is normally ruled by __str__() method, which for a built-in like list is immutable and cannot be changed to do something else.

In order to solve your problem properly define a class that works like a list and when multiplied (your *5) returns something that prints as you want it:

board = ["O"]

class Lala:
    def __init__(self, lalala, x):
        self.lalala = lalala
        self.x = x

    def __str__(self):
        res = '['
        for i in range(self.x):
            for l in self.lalala:
                if i != 0:
                    res += ' '
                res += str(l)
                if i != self.x-1:
                    res += ',\n'
        res += ']'
        return res

class Lalala(list):
    def __mul__(self, x):
        return Lala(self, x)


def lalala(lst):
    new_list = Lalala()
    for item in lst:
        new_list.append([item]*5)
    return new_list

print(lalala(board)*5)

this gives:

[['O', 'O', 'O', 'O', 'O'],
 ['O', 'O', 'O', 'O', 'O'],
 ['O', 'O', 'O', 'O', 'O'],
 ['O', 'O', 'O', 'O', 'O'],
 ['O', 'O', 'O', 'O', 'O']]

Depending on your usage of lalala(board)*5 you may need to define other methods (__getitem__, etc.) as well.

Upvotes: 0

Marc vT
Marc vT

Reputation: 506

As mentioned by other users, I do not believe that you can create a list which is printed on multiple lines. If the goal is simply to print the lines as a grid and you have to use the range() function then maybe you should try:

list1 = [['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]
for y in range(0, len(list1)):
    print list1[y]

Obviously this is not the most efficient way to solve the problem as you could completely do without the range() function but it is the only way I see a range() function being used.

Upvotes: 0

Raymond Hettinger
Raymond Hettinger

Reputation: 226754

In a way, your code is already correct. There is in fact a 5x5 list of lists containing Os.

The print statement just shows that list of lists on one line.

You could use pprint instead:

from pprint import pprint
pprint(board)

Upvotes: 2

Jean-François Fabre
Jean-François Fabre

Reputation: 140307

If you create a list of list, you cannot control how the linefeeds are issued afterwards. I would control the output line by line, using join and artificially adding the higher level [] manually

How about:

z = lalala(board)
print("["+",\n ".join(str(z) for _ in range(5))+"]")

result:

[['O', 'O', 'O', 'O', 'O'],
 ['O', 'O', 'O', 'O', 'O'],
 ['O', 'O', 'O', 'O', 'O'],
 ['O', 'O', 'O', 'O', 'O'],
 ['O', 'O', 'O', 'O', 'O']]

Upvotes: 2

Related Questions