0

I am trying to print a list of lists in python using for loops. I am having issues doing so. Ive tried a couple of approaches. Here is what I have:

for r in range(len(priceChart)-1):
    for c in range(len(priceChart[0])):
        print(priceChart[r][c], end= " ")
    print()    

I just want each element of the list of lists to be printed out without the brackets or spaces. Thanks!

Please note: I need to use for loops!

Thanks so much!

logankilpatrick
  • 13,148
  • 7
  • 44
  • 125

1 Answers1

3

Easiest to use for-each loops:

for l in priceChart:
    for c in l:
        print(c, end= " ")
    print()    

If you have to use indexes:

for r in range(len(priceChart)):  # no '- 1' here, range(x) loops from 0 to x-1
    for c in range(len(priceChart[r])):  # use 'r', not 0
        print(priceChart[r][c], end= " ")
    print()  
user2390182
  • 72,016
  • 6
  • 67
  • 89
  • Perfect that makes sense! Do you also know how to format it so that each time I print and element it uses a default amount of space.(I am printing a list of list with each element having a varying length and I want it to look good) – logankilpatrick Oct 22 '17 at 15:03
  • @logankilpatrick - [the documentation](https://docs.python.org/3/library/string.html#format-string-syntax) is your friend. – wwii Oct 22 '17 at 15:05
  • @logankilpatrick Check out the [`rjust`, `ljust`, `center`](https://docs.python.org/2/library/string.html#string.rjust) methods! – user2390182 Oct 22 '17 at 15:06
  • OkI will check it out – logankilpatrick Oct 22 '17 at 15:07
  • @logankilpatrick i have edited my answer to include your request – ragardner Oct 22 '17 at 15:09