list index out of range error while iterating in list length

I have the following code, which sortings_list consist of 2 items like

sortings_list = ['code', 'name']


for i in xrange(0, len(sortings_list)):
        if sortings_list[i] == '-%s' % field:
            sortings_list.pop(i)

Any ideas ?

Answers


You are removing items from a list while iterating, if you remove the first item then the second item's index changes. Use a list comprehension instead:

sortings_list = [elem for elem in sortings_list if not elem == '-%s' % field]

Need Your Help

Fill in values in XML Template

c# .net xml

I have xml templates stored in files where there are values to be filled are written as lets say following for example

Can I use a selector engine like sizzle to compare css selectors?

javascript jquery css css-selectors sizzle

Here's my problem: I'm writing a WordPress plugin that helps budding CSS authors see how css applies to their theme in real time. It's got a numer of nifty features, except one, which is pretty c...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.