0

I am trying to write a sequence to take a one dimensional list (regardless of size) and print it out in 2 columns. So far, what I have keeps returning an "Index out of range" error.

Ideally, I'd like for it to print the [0] index out as the header (which I've written into the sequence already) and then for the rest of the indices to be printed out like this:

- - - - - - - - - - - - - - - - - - - - - - - - -
                     NAMES
- - - - - - - - - - - - - - - - - - - - - - - - -
     Rudolph S.                    Vena U.
     Josef M.                      Efrain L.
     Joye A.                       Mee H.
     Joni M.                       Tanya E.
     Rachelle L.                   Garrett H.

So far, I have seen a lot of examples of using .zip(*) and other ways of doing something similar but usually involving a 2-dimensional list which I do not have. Does anyone have any idea how this could be improved upon?

spacing = '- ' * 25
data_list = [ "NAMES",
              "Rudolph S.",
              "Josef M.",
              "Joye A.",
              "Joni M.",
              "Rachelle L.",
              "Vena U.",
              "Efrain L.",
              "Mee H.",
              "Tanya E.",
              "Garrett H."]
while True:

    fname_prompt = input("First Name: ").strip().capitalize()

    if fname_prompt == "List" or fname_prompt == "list" or fname_prompt == "LIST":
        for item, val in enumerate(data_list):
            if item == 0:
                print(spacing)
                print('{:>27s}'.format(str(data_list[item])))
                print(spacing)

            else:
                if item <= len(data_list):
                    print('{:<10s}'.format(str(data_list[item]) + '{:>20s}'.format(str(data_list[item +1]))))
                else:
                    break
Latika Agarwal
  • 973
  • 1
  • 6
  • 11
McBraunie
  • 107
  • 1
  • 12
  • You may find some of the table-related packages [here](https://stackoverflow.com/a/26937531/9348376) useful. – Sean Breckenridge May 21 '18 at 06:17
  • Possible duplicate of [How do you split a list into evenly sized chunks?](https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks) – Sean Breckenridge May 21 '18 at 06:22

3 Answers3

1

You could try something like this with format:

print("{0:^40}".format(data_list[0]))
for i in range(1,len(data_list), 2):
   name1 = data_list[i]
   name2 = data_list[i+1]
   print("{0:<20}{1:>20}\n".format(name1, name2))
lpjune
  • 13
  • 3
1

Hope the below code helps.

if fname_prompt.lower() == "list":
    for item, val in enumerate(data_list):
        if item == 0:
            print(spacing)
            print('{:>27s}'.format(str(data_list[item])))
            print(spacing)

        else:
            cut_off = int((len(data_list)+1)/2)
            for i in range(1,int(len(dat_list)/2)):
                print (data_list[i], '\t\t\t' , data_list[cut_off+i])

You can change the first if (item==0)condition also, but i would leave it up to you to keep in however way you wanted it.Hope it helps.

Happy Coding :)

Strik3r
  • 1,052
  • 8
  • 15
1

The more-itertools package does this. Specifically the divide method.

bentheiii
  • 451
  • 3
  • 15