2

I have a simple program here in which I am trying to move numbers from listA to listB if their values are higher than 10. But how can I remove it from listA after it has moved to listB?

listA = [1,3,10,12,50,52]
listB = []
for i in range(len(listA)):
    val = listA[i]
    if val > 10:
        listB.append(val)
print(listA, listB)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Chubee
  • 87
  • 1
  • 8

6 Answers6

7

You can use the 'del' keyword. i.e., del listA[i]. However, you shouldn't do this for 2 reasons:

  1. Deleting a random element from a list is not efficient
  2. You shouldnt mutate the list while you are iterating over it. Here is what happens if I modify the list while iterating over it, notice it doesn't see 2 elements!
>>> a = [1, 2, 3, 4]
>>> for i, x in enumerate(a):
...   print(x)
...   del a[i]
... 
1
3

It would better to create a temporary list, say tmp_listA, append to it, then replace it to the original. Don't modify the original.

Here is what that would look like:

listA = [1,3,10,12,50,52]
listB = []
tmpA = []
for i in range(len(listA)):
    val = listA[i]
    if val > 10:
        listB.append(val)
    else:
        tmpA.append(val)

listA = tmpA
print(listA, listB)
Sush
  • 1,169
  • 1
  • 10
  • 26
  • This is way different than what i wrote though – Chubee Sep 18 '20 at 21:36
  • haha you're right, I just provided a snippet to illustrate the point. Modified the answer to make it more helpful now, hopefully. – Sush Sep 18 '20 at 21:41
  • 1
    Sure! Hope you look into the enumerate keyword, makes the code more readable. https://stackoverflow.com/a/22171593/4233629 – Sush Sep 18 '20 at 21:47
2

To give an alternative way:

listA = [1,3,10,12,50,52]
listB = [x for x in listA if not x>10]
listA = [x for x in listA if x>10]

print(listB)
# [1, 3, 10]

print(listA)
# [12, 50, 52]
```
AnsFourtyTwo
  • 2,480
  • 2
  • 13
  • 33
  • although its solving the purpose, its still keeping the original list and memory associated. Question is to remove the elements – Anmol Parida Sep 18 '20 at 21:54
2

I would use a simple list comprehension.

listA = [1, 3, 10, 12, 50, 52]

listB = [item for item in listA if item > 10]
listA = [item for item in listA if item not in listB]

print(listA, listB)
#[1, 3, 10] [12, 50, 52]
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
0

You can do this by using list.remove(value)

listA = [1,3,10,12,50,52,2,10,3,34]
listB = []

for i in range(len(listA)):
    if listA[i] > 10:
        listB.append(listA[i])

for val in listB:
    listA.remove(val)

print(listA, listB)
Anmol Parida
  • 672
  • 5
  • 16
0

Here is a simple more readable, straight forward way.

listA = [1,3,10,12,50,52]
listB = []
listA2 = []

for val in listA:
    if val > 10:
        listB.append(val)
    else:
        listA2.append(val)

listA = listA2

print(listA, listB)

Output:

[1, 3, 10] [12, 50, 52]

WMRamadan
  • 974
  • 1
  • 11
  • 25
-2

Here is your program a bit simplified and removing those values:

listA = [1, 3, 10, 12, 50, 52]
listA2 = listA.copy()
listB = []
for val in listA2:
    if val > 10:
        print(val)
        listB.append(val)
        listA.remove(val)
print(listA, listB)

[1, 3, 10] [12, 50, 52]
Łukasz
  • 127
  • 1
  • 5