#πŸ”’ How can i make lists appear horizontally aligned?

17 messages Β· Page 1 of 1 (latest)

kind mulch
#

I wrote this code with a couple of nested for loops, following instructions from the book im learning from "automate the boring stuff with python" and my output is vertical and not horizontally alligned:

#write a function that takes a list of lists of strings, and displays them in a well organized table with each
#column right justified. Assume the lists in list have the same number of items. (redundant?)

tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]

def printTable(data):

#This is just for finding the longest lengths in the thing.

arrayOfLengths = [0]
length = 0
for list in data:
    for words in list:
        counter = 0
        if length < len(words):
            length = len(words)
            arrayOfLengths.insert(counter, length)
        
        counter += 1
    
#now to actually print the list
for list in data:
    for words in list:
        positionInIndex = 0
        print(words.rjust( arrayOfLengths[positionInIndex]))
        positionInIndex += 1

    print()

printTable(tableData)

Also if you have any tips on how to make the program more efficient please do share!
Thank you!
ora1000x

dull wrenBOT
#

@kind mulch

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

ripe roost
#

!code

dull wrenBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

crystal raptor
vagrant void
# kind mulch I wrote this code with a couple of nested for loops, following instructions from...

OP's post fixed:
I wrote this code with a couple of nested for loops, following instructions from the book im learning from "automate the boring stuff with python" and my output is vertical and not horizontally alligned:

#write a function that takes a list of lists of strings, and displays them in a well organized table with each
#column right justified. Assume the lists in list have the same number of items. (redundant?)

tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]

def printTable(data):
    
    #This is just for finding the longest lengths in the thing.

    arrayOfLengths = [0]
    length = 0
    for list in data:
        for words in list:
            counter = 0
            if length < len(words):
                length = len(words)
                arrayOfLengths.insert(counter, length)
            
            counter += 1
        
    #now to actually print the list
    for list in data:
        for words in list:
            positionInIndex = 0
            print(words.rjust( arrayOfLengths[positionInIndex]))
            positionInIndex += 1

        print()

printTable(tableData)

Also if you have any tips on how to make the program more efficient please do share!
Thank you!
ora1000x

#

@kind mulch you can actually use str.ljust or str.rjust to make it, don't know if your book suggests that but thats built in to the string class.

#

rjust is probably proper for your case since you want to right justify it

kind mulch
ripe roost
#

you're already calling it? words.rjust()

#

str.rjust() just means the rjust method of the str type

#

also clobbering built-in names is bad practice (for list in data:)

kind mulch
#

Ohhh, i see what @vagrant void meant now, but i wanted to do it relative to the positionInIndex var i created, when i remove the posinIndex var and try running rjust() alone it gives me the memory address

sick shell
# kind mulch Ohhh, i see what <@448782473359392788> meant now, but i wanted to do it relative...

You're printing the function itself. Essentially you're doing:

def f() -> int:
    return 4

print(f)  # <-- you're doing this. This will print "information" about the function object itself
print(f())  # <-- this will evaluate the function and print "information" about whatever is returned by this function.

Note that the return value of a function can be another function, e.g.:

def f() -> int:
    return 4

def g():
    return f

print(f)  # prints "information" about f
print(g)  # prints "information" about g
print(f())  # prints "information" about the return value of f, i.e. '4'
print(g())  # prints "information" about the return value of g, i.e. "information" about f
print(g()())  # prints "information" about the return value of the return value of g, which is the return value of f, which is '4'
#

This behaviour, by extension, will also lead us to decorators, e.g.:

def memoize(func):
    memory: dict = {}
    def wrapper(*args, **kwargs):
        key = f"{args}{kwargs}"
        if key not in memory:
            memory[key] = func(*args, **kwargs)

        return memory[key]

    return wrapper


@memoize
def fib(n: int) -> int:
    if n < 2:
        return n

    return fib(n - 1) + fib(n - 2)


print(fib(5))
print(fib(100))  # with the @memoize it calculates instantly, without it...

But that may be a bit too much for now

dull wrenBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.