-2
class Sample:

    def __init__(self):
        self.lst_report_footer_strings = ['Manager', 'Accountant', 'Created By', 
                                         'fifth', '', '']
        int_size_of_string = 0
        lst_temp_report_footer = self.lst_report_footer_strings
        for lst_report_footer_item in self.lst_report_footer_strings:
            print lst_temp_report_footer
            print lst_report_footer_item
            if lst_report_footer_item in ('', ' '):
                print "Inside if : Item ==" + lst_report_footer_item
                lst_temp_report_footer.remove(lst_report_footer_item)   
                print "list after remove == " + str(lst_temp_report_footer)
            else:
                print "Inside else : length = ", str(len(lst_report_footer_item))
                int_size_of_string += len(lst_report_footer_item)


if __name__ == '__main__':
    ins_class = Sample()

Output

['Manager', 'Accountant', 'Created By', 'fifth', '', '']
Manager
Inside else : length =  7
['Manager', 'Accountant', 'Created By', 'fifth', '', '']
Accountant
Inside else : length =  10
['Manager', 'Accountant', 'Created By', 'fifth', '', '']
Created By
Inside else : length =  10
['Manager', 'Accountant', 'Created By', 'fifth', '', '']
fifth
Inside else : length =  5
['Manager', 'Accountant', 'Created By', 'fifth', '', '']

Inside if : Item ==
list after remove == ['Manager', 'Accountant', 'Created By', 'fifth', '']

What i need is....

list after remove == ['Manager', 'Accountant', 'Created By', 'fifth']
3
  • @Igor thanks for that edit, had hard time understand the OP and his code. Commented Aug 1, 2012 at 10:48
  • @lagor : thanks for the edit. this is my first time i am posting a code. sorry Commented Aug 1, 2012 at 11:00
  • if you have found a solution, from one of these answers (which i think you have) please mark it as such. i only remind you of this because you mentioned you are new to the site. Commented Aug 1, 2012 at 11:48

3 Answers 3

1

I have turned this into a much simpler piece of code and tried to work out what you are really trying to do. There is a whole lot of unecessary code and why would you use a class to illustrate your problem?

>>> x=['Manager', 'Accountant', 'Created By', 'fifth', '', '']
>>> result = [i for i in x if i.strip()]
>>> result
['Manager', 'Accountant', 'Created By', 'fifth']
>>>
Sign up to request clarification or add additional context in comments.

1 Comment

@ Tim : Thanks.. i used a class only to specify that the code inside the class is a portion of my actual program and the problem is that when i run it on the terminal it works as i needed.. but when i use this code in my .py file it does not works. Thank you very much for the solution
1

This is equivalent to your class, sans debugging prints, and with a shortened variable name.

class Sample:
   def __init__(self):
       self.footers = ['Manager', 'Accountant', 'Created By', 'fifth', '', '']
       self.footers = [x for x in self.footers if x.strip() != ""]
       self.int_size_of_string = sum(len(x) for x in self.footers)

if __name__ == '__main__':
    myclass = Sample()
    print myclass.footers
    print myclass.int_size_of_string

I've also made int_size_of_string an attribute, since otherwise it is not accessible once __init__ has completed (you cannot return a value from __init__). The strip method removes any number of spaces and other whitespace characters from both ends of the string.

The reason your code didn't work was because you were modifying your list as you were iterating over it. You removed the second to last item, so the last item took its place, and then when it moved on to the next item, there were no more items left.

4 Comments

@ Lazyr : Thanks.. I used two list to solve the problem you specified, one to iterate and other to remove item (lst_temp_report_footer) and assign the temp to the original after the iterations. Thank you for your quick responce :)
edited the answer to add main
@Jyohtish No you didn't. lst_temp_report_footer is the same list as self.lst_report_footer_strings. To make a copy, use list(self.lst_report_footer_strings) or self.lst_report_footer_strings[:].
@ lazyr : Thank you lazyr.. i am new to python and i was unaware about this problem.. :-) Thank you very much
0

okay, the reason it is not working is because your loop goes over each item in your list. when it finds a space, it removes that item. and displays your current list. so the first time, it will show you the space since there are 2 of them.

BUT, now your loop has failed because you have modified the list while in the loop and so there are no more items for it to iterate around, since you were at item 5 out of 6, and then you removed item 5, now the list only contains 5 items, but the loop seeks item 6. since there is none, it exits. leaving the list alone as it is.

essentially, what you should do is the following:

class Sample:
   def __init__(self):
       self.lst_report_footer_strings = \
['Manager', 'Accountant', 'Created By', 'fifth', '', '']
       int_size_of_string = 0
       lst_temp_report_footer = self.lst_report_footer_strings
       temp_remove_list = []
       for lst_report_footer_item in xrange(len(self.lst_report_footer_strings)):
           print lst_temp_report_footer
           print self.lst_report_footer_strings[lst_report_footer_item]
           if lst_temp_report_footer[lst_report_footer_item] in ['',' ']:
               print "Inside if : Item =="+self.lst_report_footer_strings[lst_report_footer_item]
               temp_remove_list.append(lst_report_footer_item)   
           else:
               print "Inside else : length = ",str(len(lst_temp_report_footer[lst_report_footer_item]))
               int_size_of_string += len(lst_temp_report_footer[lst_report_footer_item])

       for item in reversed(temp_remove_list):
           del lst_temp_report_footer[item]

       print "final list : == ",lst_temp_report_footer

if __name__ == '__main__':
    ins_class = Sample()

note - there was something strange about the tabs in this file, i hope it sorts out for you.

2 Comments

yes, the other answers are much more elegant, i was trying to fix the OP code... silly me.. :)
@ Inbar : sorry for the miss placed tabs.. This is my first sample code i am uploading.. so the alignment was tough for me.. :-P

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.