Python novice list - remove I have a question Peep πŸ₯🐀🐣

Asked 2 years ago, Updated 2 years ago, 138 views

When you want to remove a common number in the list

a = [1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7]

for remove_one in a:

    a.remove(1)

print(a)
[2, 3, 4, 5, 6, 7]

All 1s are removed here


a = [1, 2, 1, 1, 1, 1, 1, 1, 1, 1]

for remove_one in a:

    a.remove(1)

print(a)
[2, 1, 1, 1, 1]

Why are there a few more at the back of the ship?

python list remove

2022-09-20 15:30

1 Answers

If you do remove (value) in the list, If there are multiple values, only the first value is deleted.

The for statement replaces each element with a variable sequentially in the list.

The for statement ends after substituting the last element of the list.

But the above example is a really exquisite trick. Haha I think it will be a good study material for the list.

Example 1

a = [1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7]
for remove_one in a:
    a.remove(1)
In code on

from the one remove (It's like, for in a, but it looks like :)

.

At first, one would be substituted.

That 1 is removed by remove, from the lista

Then the next element (2nd) will be substituted for remove_one

The first one is gone

At the time of the second performance, a is

[2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7] It'll be like this.

Remove_one contains the second element, 1.

When the remove(1) command is executed,

Out of the remaining 1, the first one is deleted again.

a becomes [2, 3, 1, 4, 1, 5, 1, 6, 1, 7].

(Remove_one contains the third element, 1.)

If repeated this way,

Well, anyway, when I went all the way to the end of the list, remove (1) continued to be carried out.

Eventually a = [2, 3, 4, 5, 6, 7]

It goes like this.

a The length of a was originally 12, but it was reduced to 6. Remember.

But the code below is different.

Example 2

a = [1, 2, 1, 1, 1, 1, 1, 1, 1, 1]
for remove_one in a:
    a.remove(1)

It will work as explained above.

This time, a is 10 in length and 1 in number is 9.

If it's as intended, I'll have to turn 10 times

The remove command keeps decreasing the length of a.

First: [1, 2, 1, 1, 1, 1, 1, 1, 1, 1]

After performing the first for statement: [2, 1, 1, 1, 1, 1, 1, 1, 1]

After performing the 2nd for statement: [2, 1, 1, 1, 1, 1, 1, 1]

After performing the 3rd for statement: [2, 1, 1, 1, 1, 1, 1]

After performing the 4th for statement: [2, 1, 1, 1, 1, 1]

After performing the 5th for statement: [2, 1, 1, 1, 1]

Now, shall we do the 6th remove? No, the for statement is aborted.

Currently, the size of list a is 5, but we can't get the next element.

That's why the removal was stopped after 5 times.

It's kind of lacking...

Add code to remove all desired values from the list.

Please keep that in mind ^

^
# I will remove all BMWs. If you just turn the for statement, an error such as the example number 2 above will occur.
carList = ['BMW', 'BENZ', 'BMW', 'BMW', 'BMW', 'BMW', 'BMW', 'BMW', 'AUDI', 'BMW', 'BMW']

# Errors
# # for car in carList:
#     #     if car == 'BMW':
#         #         carList.remove(car)
# # print(carList)
# # ['BENZ', 'BMW', 'BMW', 'AUDI', 'BMW', 'BMW']

# the summit
count = carList.count('BMW')
for _ in range(count):
    carList.remove('BMW')
print(carList)
# # ['BENZ', 'AUDI']


2022-09-20 15:30

If you have any answers or tips


Β© 2024 OneMinuteCode. All rights reserved.