Python: Printing a list without the brackets and single quotes?

15,953

Each item in the list is itself a singleton list. There's propably no reason for this - if you can't name one, go and remove them (by using re.find over re.findall or returning a single item from the list returned by re.findall), they're just redundant and cause trouble like in this case.

Regardless, print item[0] should work as it's printing the single element in the list, and unlike the str() of lists, it won't run the item through repr first (which causes the quotes and would escape unprintable characters if there were any in the string). And once you got rid of the redundant singleton lists, print '\n'.join(items) will work as well.

Your code throws an error if there is an empty list in theList. If there is a line in recentFile that does not contain anything formatted like an IP, an empty list will be returned by returnIP, and if any line in comparisonFile (by the way: you open it with a descriptive name at the beginning, but open it again and again without a descriptive name in chechMatch) contains no IP address either, you'll get another empty list which of course equals the empty list passed as parameter ip. So for non-IP names in recentFile, empty lists will be added. This whole troubel can be avoided if you return strings instead of singleton lists from returnIP, use None when there is no IP in the current line, and skip the checking/appending in compareFiles if returnIP returns None.

Share:
15,953
Dan
Author by

Dan

Updated on June 18, 2022

Comments

  • Dan
    Dan about 2 years

    I have a list full of IP addresses. I would like to iterate through the list and print each IP address. When I try doing this:

    def printList(theList):
        for item in theList:
            print item
    

    And the output looks like this:

    ['8.0.226.5']
    ['8.0.247.5']
    ['8.0.247.71']
    ['8.0.249.28']
    ['8.0.249.29']
    

    I have tried everything, including "print item[0]" in the loop. What am I doing wrong?

    • Greg Hewgill
      Greg Hewgill about 13 years
      Really? What happened when you tried print item[0]?
    • Dan
      Dan about 13 years
      IndexError: list index out of range
    • Håvard
      Håvard about 13 years
      Could you show us what this list looks like?
    • Admin
      Admin about 13 years
      That error can only occur if a list is empty. The lists you printed are all non-empty.
    • Dan
      Dan about 13 years
      Doing repr(item) does the same thing. Delnan, the list isn't empty, but apparently it thinks the "item" in the loop isn't a list. (Even though it sure is printing like one).
    • Admin
      Admin about 13 years
      @Dan: One list you're trying to take the item with index 0 from must be empty. I see that what you printed is not empty, but index 0 is only out of range when there isn't a (0+1)th item, i.e. when the list is empty, so if you get that error there must be an empty list, period. Show the code and exampel data (print repr(theList) should give you an exact representation of the list) that causes the error (e.g. on pastebin.com).
    • Dan
      Dan about 13 years
      Think you could take a look? pastebin.com/as1mt09s
  • Dan
    Dan about 13 years
    I figured it out. Apparently at the top of the list had a blank item, so it was stopping there. Thanks for your help! Edit: I'm not a member, or I'd upvote you :(
  • Admin
    Admin about 13 years
    @Dan: Added detailed explanation why it happens and suggestions how to resolve it. A bit late, but if any of it helps... :)
  • Dan
    Dan about 13 years
    Thanks! For the comparisonFile thing, I was originally referencing it later on but for some reason it wouldn't work when using the variable name so I tried replacing it with the open function inline and it worked fine. I'm not sure why this happened. I might try to recreate the problem and try to figure it out.