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 ?
You are better off using list comprehension because indexing is messy. With Python, you don't need to index a list in most cases. That being said, if you still insist on using your solution:
for i in xrange(len(sortings_list) - 1, -1, -1):
if ...:
sortings_list.pop(i)
That is, you start from the end of the list and traverse backward. That way, all the indexing still works. Again, I highly recommend against doing things this way. Go with list comprehension which Martijn Pieters offers.