Python skipping last element in while iterating a list using for loop [duplicate]

2024/11/17 5:51:51
class Sample:def __init__(self):self.lst_report_footer_strings = ['Manager', 'Accountant', 'Created By', 'fifth', '', '']int_size_of_string = 0lst_temp_report_footer = self.lst_report_footer_stringsfor lst_report_footer_item in self.lst_report_footer_strings:print lst_temp_report_footerprint lst_report_footer_itemif lst_report_footer_item in ('', ' '):print "Inside if : Item ==" + lst_report_footer_itemlst_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']
Answer

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']
>>>
https://en.xdnf.cn/q/120239.html

Related Q&A

can this code be shortened or improved? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about programming within the scope defined in the help center.Cl…

Count Running and Stopped Ec2 Instances with AWS Lambda

How can I count number of running and stopped EC2 instances in a particular region using boto3 and an AWS Lambda function?

Python2.7: How to split a column into multiple column based on special strings like this?

Im a newbie for programming and python, so I would appreciate your advice!I have a dataframe like this.In info column, there are 7 different categories: activities, locations, groups, skills, sights, t…

python - return and print does not give same result

what Im trying to do is take the entered string separated by commas and change it to list then for each list as a key print the associated value.def main():myDict = {a:1, b:2, c:3, d:4, e:5....}u_input…

Creating a menuBar in a frame?

import Tkinter as tk from Tkinter import Label, StringVar, Entry, Button, Menu, Frame, Tk, Canvas import subprocess from Tkconstants import LEFT, CENTER,W, SUNKEN , X, Yclass SampleApp(tk.Tk):def __ini…

Python return dictionary [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Use selenium and python to move mouse outside of web page area

I need to test the exit intent form on this page: https://www.graphicproducts.com/guides/5s-system/When the mouse pointer is moved outside of the web page area a popup window appears. I then need to en…

Python „stack overflow” (104 if statements). Is def(x) the only solution to optimise code?

Today have tried to check files with path directory name. Previously it worked, until I tried to create 104 if/else statements. How to dispose of this overflow error? More specific question: Does def(…

How to fix Python restarting whenever I start program on IDLE environment?

import randomwinning_conditon = 0 no_of_guesses = 0 comp_guess = random.randint(1,100)while (no_of_guesses == 11) or (winning_conditon == 1):user_guess = int(input("What is your guess? "))if…

Fetching Lawyers details from a set of urls using bs4 in python

I am an absolute beginner to Web Scraping using Python and know very little about programming in Python. I am just trying to extract the information of the lawyers in the Tennessee location. In the web…