5

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?

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
Dan
  • 451
  • 2
  • 5
  • 4
  • 9
    Really? What happened when you tried `print item[0]`? – Greg Hewgill Apr 21 '11 at 21:38
  • IndexError: list index out of range – Dan Apr 21 '11 at 21:43
  • 3
    Could you show us what this list looks like? – Håvard Apr 21 '11 at 21:43
  • That error can only occur if a list is empty. The lists you printed are all non-empty. –  Apr 21 '11 at 21:43
  • 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). – Dan Apr 21 '11 at 21:46
  • 2
    @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 http://www.pastebin.com). –  Apr 21 '11 at 21:48
  • Think you could take a look? http://pastebin.com/as1mt09s – Dan Apr 21 '11 at 21:50

2 Answers2

6

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.

  • 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 :( – Dan Apr 21 '11 at 21:58
  • @Dan: Added detailed explanation why it happens and suggestions how to resolve it. A bit late, but if any of it helps... :) –  Apr 21 '11 at 22:02
  • 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. – Dan Apr 21 '11 at 22:07
0

I think theList is not a list of IPs but a list of lists of IPs(each of them with 1 element).

Another cause of the problem would be that you have an IP class with an overwritten str method that prints it like that.

x10
  • 3,820
  • 1
  • 24
  • 32