Printing a list of lists, without brackets

31,448

Solution 1

The following will do it:

for item in l:
  print item[0], ', '.join(map(str, item[1:]))

where l is your list.

For your input, this prints out

tables 1, 2
ladders 2, 5
chairs 2

Solution 2

If you don't mind that the output is on separate lines:

foo = [["tables", 1, 2], ["ladders", 2, 5], ["chairs", 2]]
for table in foo:
    print "%s %s" %(table[0],", ".join(map(str,table[1:])))

To get this all on the same line makes it slightly more difficult:

import sys
foo = [["tables", 1, 2], ["ladders", 2, 5], ["chairs", 2]]
for table in foo:
    sys.stdout.write("%s %s " %(table[0],", ".join(map(str,table[1:]))))

print

Solution 3

In Python 3.4.x

The following will do it:

for item in l:
    print(str(item[0:])[1:-1])

where l is your list.

For your input, this prints out:

tables 1, 2
ladders 2, 5
chairs 2

Another (cleaner) way would be something like this:

for item in l:
    value_set = str(item[0:])
    print (value_set[1:-1])

Yields the same output:

tables 1, 2
ladders 2, 5
chairs 2

Hope this helps anyone that may come across this issue.

Share:
31,448
user1079404
Author by

user1079404

Updated on July 25, 2022

Comments

  • user1079404
    user1079404 almost 2 years

    A somewhat similar question was asked on here but the answers did not help.

    I have a list of lists, specifically something like..

    [[tables, 1, 2], [ladders, 2, 5], [chairs, 2]]
    

    It is meant to be a simple indexer.

    I am meant to output it like thus:

    tables 1, 2
    ladders 2, 5
    chairs 2
    

    I can't get quite that output though.

    I can however get:

    tables 1 2
    ladders 2 5
    chairs 2
    

    But that isn't quite close enough.

    Is there a simple way to do what I'm asking? This is not meant to be the hard part of the program.

  • NPE
    NPE over 12 years
    @LarryLustig: As far as I can see, the output is exactly as the OP requested, down to spaces and commas.
  • user1079404
    user1079404 over 12 years
    I just tried this with: [['wind', 1, 2], ['blow', 1], ['form', 1], ['south', 1], ['strong', 2]] I got output of: w i, n, d Followed by a traceback error about why an int is not subscriptable
  • user1079404
    user1079404 over 12 years
    This pretty much makes sense, thank you for the explinations, but it also comes up with the same strange output that I responded to the first answer does. It will print something like: [['wind', 1, 2], ['blow', 1], ['form', 1], ['south', 1], ['strong', 2]] I got output of: w i, n, d Followed by a traceback error about why an int is not subscriptable
  • NPE
    NPE over 12 years
    @user1079404: I've just tested the code on your list and it works. Make sure you're applying it to the entire list, not to each element.
  • Larry Lustig
    Larry Lustig over 12 years
    @aix: My mistake, I was reading the actual, rather than desired, output.
  • user1079404
    user1079404 over 12 years
    Sorry, I see what I was doing wrong. The code works fine, but, I don't understand is the way that "join" works, so it always adds the string (in this case ', ') on to the end of each string in the iteration?
  • NPE
    NPE over 12 years
    @user1079404: It inserts the , between consecutive elements. See docs.python.org/library/stdtypes.html#str.join
  • ovgolovin
    ovgolovin over 12 years
    Also, it's possible to use end='' as a parameter in print function as of Python 3, or in Python 2 with adding from __future__ import print_function statement at the beginning.
  • ovgolovin
    ovgolovin over 12 years
    @user1079404 I don't uderstand what's wrong. The code works for me.
  • user1079404
    user1079404 over 12 years
    Yup I get it now thanks. Had to look up the python docs for "map" too but that also makes sense. Thanks once again.
  • jordanm
    jordanm over 12 years
    from __future__ import print_function appears to be available in 2.6, but not 2.4.