It's a question about the Python list

Asked 2 years ago, Updated 2 years ago, 103 views

Thank you for pointing out the idea of eating raw in the previous question.
I solved the part you gave me!0^
I have a new question.
Assuming that data a has 1, 2, and 3 and the number b has 1, if b is included in a, we return the value of a minus b, and if a does not have b, we return 0. The transition function algorithm is

0^
if element_value in list_data:
        print(list_data.remove(element_value))
    else:
        print("0")

That's what I set up.

After that, the list_data value was given 1, 2, 3 and element_value was given 1.

I think it should be 2 and 3 minus 1 from 1, 2, 3, but the else value is 0. Can you explain what's wrong with this function? Thank you so much for helping me with the previous question.

python atom

2022-09-22 10:50

1 Answers

>>> l = [ 1, 2, 3 ]
>>> help(l.remove)
Help on built-in function remove:

remove(...) method of builtins.list instance
    L.remove(value) -> None -- remove first occurrence of value.
    Raises ValueError if the value is not present.

The list.remove method returns None.

print(a.remove(b))

print returns the value of a.remove. Therefore, b will fall out of a, but None will be taken.


2022-09-22 10:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.