Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for tup in somelist:
- if determine(tup):
- code_to_remove_tup
- somelist = [x for x in somelist if not determine(x)]
- somelist[:] = [x for x in somelist if not determine(x)]
- from itertools import ifilterfalse
- somelist[:] = ifilterfalse(determine, somelist)
- from itertools import filterfalse
- somelist[:] = filterfalse(determine, somelist)
- somelist[:] = [tup for tup in somelist if determine(tup)]
- for tup in somelist[:]:
- etc....
- >>> somelist = range(10)
- >>> for x in somelist:
- ... somelist.remove(x)
- >>> somelist
- [1, 3, 5, 7, 9]
- >>> somelist = range(10)
- >>> for x in somelist[:]:
- ... somelist.remove(x)
- >>> somelist
- []
- for i in xrange(len(somelist) - 1, -1, -1):
- if some_condition(somelist, i):
- del somelist[i]
- somelist = [tup for tup in somelist if determine(tup)]
- newlist = []
- for tup in somelist:
- # lots of code here, possibly setting things up for calling determine
- if determine(tup):
- newlist.append(tup)
- somelist = newlist
- for tup in somelist[:]:
- # lots of code here, possibly setting things up for calling determine
- if determine(tup):
- newlist.append(tup)
- somelist[:] = filter(lambda tup: not determine(tup), somelist)
- from itertools import ifilterfalse
- somelist[:] = list(ifilterfalse(determine, somelist))
- >>> for w in words[:]: # Loop over a slice copy of the entire list.
- ... if len(w) > 6:
- ... words.insert(0, w)
- ...
- >>> words
- ['defenestrate', 'cat', 'window', 'defenestrate']
- for x in a[:]:
- if x < 0: a.remove(x)
- for item in originalList:
- if (item != badValue):
- newList.append(item)
- originalList[:] = newList
- array = [lots of stuff]
- arraySize = len(array)
- i = 0
- while i < arraySize:
- if someTest(array[i]):
- del array[i]
- arraySize -= 1
- else:
- i += 1
- >>> L1 = [(1,2), (5,6), (-1,-2), (1,-2)]
- >>> for (a,b) in L1:
- ... if a < 0 or b < 0:
- ... L1.remove(a,b)
- ...
- Traceback (most recent call last):
- File "<stdin>", line 3, in <module>
- TypeError: remove() takes exactly one argument (2 given)
- >>> L1[1]
- (5, 6)
- >>> type(L1[1])
- <type 'tuple'>
- # The remove line now includes an extra "()" to make a tuple out of "a,b"
- L1.remove((a,b))
- L1 is now: [(1, 2), (5, 6), (1, -2)]
- L1 = [(1,2),(5,6),(-1,-2),(1,-2),(3,4),(5,7),(-4,4),(2,1),(-3,-3),(5,-1),(0,6)]
- ### Outputs:
- L1 is now: [(1, 2), (5, 6), (1, -2), (3, 4), (5, 7), (2, 1), (5, -1), (0, 6)]
- L2 = L1
- for (a,b) in L1:
- if a < 0 or b < 0 :
- L2.remove((a,b))
- # Now, remove the original copy of L1 and replace with L2
- print L2 is L1
- del L1
- L1 = L2; del L2
- print ("L1 is now: ", L1)
- 'L1 is now: ', [(1, 2), (5, 6), (1, -2), (3, 4), (5, 7), (2, 1), (5, -1), (0, 6)]
- >>> L2=L1
- >>> L1 is L2
- True
- import copy
- L1 = [(1,2), (5,6),(-1,-2), (1,-2),(3,4),(5,7),(-4,4),(2,1),(-3,-3),(5,-1),(0,6)]
- L2 = copy.copy(L1)
- for (a,b) in L1:
- if a < 0 or b < 0 :
- L2.remove((a,b))
- # Now, remove the original copy of L1 and replace with L2
- del L1
- L1 = L2; del L2
- >>> L1 is now: [(1, 2), (5, 6), (3, 4), (5, 7), (2, 1), (0, 6)]
- L1 = [(1,2), (5,6),(-1,-2), (1,-2),(3,4),(5,7),(-4,4),(2,1),(-3,-3),(5,-1),(0,6)]
- for (a,b) in reversed(L1):
- if a < 0 or b < 0 :
- L1.remove((a,b))
- print ("L1 is now: ", L1)
- >>> L1 is now: [(1, 2), (5, 6), (3, 4), (5, 7), (2, 1), (0, 6)]
- inlist = [{'field1':10, 'field2':20}, {'field1':30, 'field2':15}]
- for idx, i in enumerate(inlist):
- do some stuff with i['field1']
- if somecondition:
- xlist.append(idx)
- for i in reversed(xlist): del inlist[i]
- list_len = len(some_list)
- for i in range(list_len):
- reverse_i = list_len - 1 - i
- cur = some_list[reverse_i]
- # some logic with cur element
- if some_condition:
- some_list.pop(reverse_i)
- k = range(5)
- v = ['a','b','c','d','e']
- d = {key:val for key,val in zip(k, v)}
- print d
- for i in range(5):
- print d[i]
- d.pop(i)
- print d
- from fluidIter import FluidIterable
- fSomeList = FluidIterable(someList)
- for tup in fSomeList:
- if determine(tup):
- # remove 'tup' without "breaking" the iteration
- fSomeList.remove(tup)
- # tup has also been removed from 'someList'
- # as well as 'fSomeList'
- from fluidIter import FluidIterable
- l = [0,1,2,3,4,5,6,7,8]
- fluidL = FluidIterable(l)
- for i in fluidL:
- print('initial state of list on this iteration: ' + str(fluidL))
- print('current iteration value: ' + str(i))
- print('popped value: ' + str(fluidL.pop(2)))
- print(' ')
- print('Final List Value: ' + str(l))
- initial state of list on this iteration: [0, 1, 2, 3, 4, 5, 6, 7, 8]
- current iteration value: 0
- popped value: 2
- initial state of list on this iteration: [0, 1, 3, 4, 5, 6, 7, 8]
- current iteration value: 1
- popped value: 3
- initial state of list on this iteration: [0, 1, 4, 5, 6, 7, 8]
- current iteration value: 4
- popped value: 4
- initial state of list on this iteration: [0, 1, 5, 6, 7, 8]
- current iteration value: 5
- popped value: 5
- initial state of list on this iteration: [0, 1, 6, 7, 8]
- current iteration value: 6
- popped value: 6
- initial state of list on this iteration: [0, 1, 7, 8]
- current iteration value: 7
- popped value: 7
- initial state of list on this iteration: [0, 1, 8]
- current iteration value: 8
- popped value: 8
- Final List Value: [0, 1]
- fluidL[2] = 'a' # is OK
- fluidL = [0, 1, 'a', 3, 4, 5, 6, 7, 8] # is not OK
- fluidArr = FluidIterable([0,1,2,3])
- # get iterator first so can query the current index
- fluidArrIter = fluidArr.__iter__()
- for i, v in enumerate(fluidArrIter):
- print('enum: ', i)
- print('current val: ', v)
- print('current ind: ', fluidArrIter.currentIndex)
- print(fluidArr)
- fluidArr.insert(0,'a')
- print(' ')
- print('Final List Value: ' + str(fluidArr))
- enum: 0
- current val: 0
- current ind: 0
- [0, 1, 2, 3]
- enum: 1
- current val: 1
- current ind: 2
- ['a', 0, 1, 2, 3]
- enum: 2
- current val: 2
- current ind: 4
- ['a', 'a', 0, 1, 2, 3]
- enum: 3
- current val: 3
- current ind: 6
- ['a', 'a', 'a', 0, 1, 2, 3]
- Final List Value: ['a', 'a', 'a', 'a', 0, 1, 2, 3]
- originalList = fluidArr.fixedIterable
- newList = [i for i in oldList if testFunc(i)]
- randInts = [70, 20, 61, 80, 54, 18, 7, 18, 55, 9]
- fRandInts = FluidIterable(randInts)
- fRandIntsIter = fRandInts.__iter__()
- # for each value in the list (outer loop)
- # test against every other value in the list (inner loop)
- for i in fRandIntsIter:
- print(' ')
- print('outer val: ', i)
- innerIntsIter = fRandInts.__iter__()
- for j in innerIntsIter:
- innerIndex = innerIntsIter.currentIndex
- # skip the element that the outloop is currently on
- # because we don't want to test a value against itself
- if not innerIndex == fRandIntsIter.currentIndex:
- # if the test element, j, is a multiple
- # of the reference element, i, then remove 'j'
- if j%i == 0:
- print('remove val: ', j)
- # remove element in place, without breaking the
- # iteration of either loop
- del fRandInts[innerIndex]
- # end if multiple, then remove
- # end if not the same value as outer loop
- # end inner loop
- # end outerloop
- print('')
- print('final list: ', randInts)
- outer val: 70
- outer val: 20
- remove val: 80
- outer val: 61
- outer val: 54
- outer val: 18
- remove val: 54
- remove val: 18
- outer val: 7
- remove val: 70
- outer val: 55
- outer val: 9
- remove val: 18
- final list: [20, 61, 7, 55, 9]
- alist = ['good', 'bad', 'good', 'bad', 'good']
- i = 0
- for x in alist[:]:
- if x == 'bad':
- alist.pop(i)
- i -= 1
- else:
- # do something cool with x or just print x
- print x
- i += 1
- somelist = [x for x in somelist if not determine(x)]
- for i, item in enumerate(my_list):
- if condition:
- my_list.pop(i)
Add Comment
Please, Sign In to add comment