Python Forum

Full Version: Column Print?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
What should I do to make it look like this?
String Methods List Methods Tuple Methods Dict Methods Set Methods

title() append() ...... .... .......
replace() extend() ...... ....... ........
...... ....

str_ = "String Methods"
list_ = "List Methods"
tuple_ = "Tuple Methods"
dict_ = "Dict Methods"
set_ = "Set Methods"

print(f"{str_:<20} {list_:<20} {tuple_:<20} {dict_:<20} {set_:<20}")
print()

str_list = list()
list_list = list()
tuple_list = list()
dict_list = list()
set_list = list()

for i in dir(""):
    if not i.startswith("__"):
        str_list += [i]

for i in dir([]):
    if not i.startswith("__"):
        list_list += [i]

for i in dir(tuple()):
    if not i.startswith("__"):
        tuple_list += [i]

for i in dir(dict()):
    if not i.startswith("__"):
        dict_list += [i]

for i in dir(set()):
    if not i.startswith("__"):
        set_list += [i]

total_list = []
total_list += [str_list, list_list, tuple_list, dict_list, set_list]

#for i in total_list:
#    print(*i)
You should print the lists just like the header.
print(f"{str_:<20} {list_:<20} {tuple_:<20} {dict_:<20} {set_:<20}")
...
print(f"{str_list[i]:<20} {list_list[i]:<20} {tuple_list[i]:<20} {dict_list[i]:<20} {set_list[i]:<20}")
But where does the index "i" come from? You will have to put this second print statement in a loop (for or while) where "i" increments from 0 to the maximum.
Try this and let us know what errors you get.
(Mar-22-2020, 09:27 AM)ibreeden Wrote: [ -> ]You should print the lists just like the header.
print(f"{str_:<20} {list_:<20} {tuple_:<20} {dict_:<20} {set_:<20}")
...
print(f"{str_list[i]:<20} {list_list[i]:<20} {tuple_list[i]:<20} {dict_list[i]:<20} {set_list[i]:<20}")
But where does the index "i" come from? You will have to put this second print statement in a loop (for or while) where "i" increments from 0 to the maximum.
Try this and let us know what errors you get.


TypeError: list indices must be integers or slices, not list
What are list indices? In my example "i" is the list index. So what is de value of i when the error arises?
(Mar-22-2020, 11:33 AM)ibreeden Wrote: [ -> ]What are list indices? In my example "i" is the list index. So what is de value of i when the error arises?

first try:
for i in range(0, len(total_list)+1):
    print(f"{str_list[i]:<20} {list_list[i]:<20} {tuple_list[i]:<20} {dict_list[i]:<20} {set_list[i]:<20}")
Output: list index out of range (string has "45" methods, but list has "11" methods so this is not working.)

Second try:
for i in total_list:
    print(f"{str_list[i]:<20} {list_list[i]:<20} {tuple_list[i]:<20} {dict_list[i]:<20} {set_list[i]:<20}")
list indices must be integers or slices, not list (Sorry, i didn't pay attention :) of course list indices must be integers or slices)
First you have to get rid of total_list. You can do nothing with it.
The number of rows should be equal to the largest list. So get the lengt of each list an determine what the largest is. Say you call that variable: "max_list". Then you can do:
for i in range(max_list):
    print(f"{str_list[i]:<20} {list_list[i]:<20} {tuple_list[i]:<20} {dict_list[i]:<20} {set_list[i]:<20}")
But then you say:
(Mar-22-2020, 11:48 AM)Guga Wrote: [ -> ]string has "45" methods, but list has "11" methods so this is not working.
You are right, so you have to think of something to make it work.
  • append empty strings to the lists that have less than "max_list" items, or
  • introduce new variables that you use in the printing loop and assign the current value of each list or an empty string when the list is exhausted.
An example of the last method:
for i in range(max_list):
    if len(list_list) < i:
        list_method = list_list[i]
    else:
        list_method = ""
    print(f"{list_method:<20}")