[python3] I don't know why this error appears in Remove List.

Asked 2 years ago, Updated 2 years ago, 48 views

a = ["leo", "kiki", "eden"]
b = ["eden", "kiki"]

def s(a,b):
    zip = []
    for a in b:
        zip.append(a)

    for i in range(len(zip)):
        a.remove(zip[i])

    return a
print(s(a,b))

I want to delete the list of a and print it out, but when I run it,

Traceback (most recent call last):
  File "main.py", line 13, in <module>
    print(s(a,b))
  File "main.py", line 10, in s
    a.remove(zip[i])
AttributeError: 'str' object has no attribute 'remove'

It pops up like this.

I don't understand why it's perceived as str. Isn't it list? I don't know why I can't use it.

python3 list

2022-09-22 18:26

2 Answers

def s(a,b):
    zip = []
    for a in b:              # <<<< a !
        zip.append(a)

    for i in range(len(zip)):
        a.remove(zip[i])

As the string is assigned to a in the first for statement, the list a entered as a factor is lost.


2022-09-22 18:26

a = ["leo", "kiki", "eden"]
b = ["eden", "kiki"]

def s(a,b):
    return list(set(a) - set(b))

print(s(a,b))


2022-09-22 18:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.