user10568777
user10568777

Reputation:

How can i seperate some characters from one big string?

I have created a big string and I put its characters into a list called chars. I want to separate some specific characters from it.

for example:

chars = ["ü","ç","ö","a","ş","ğ","ı","d"]
chars2 = []

Just a simple python 3.7 with pycharm on my windows

    for i in chars:
        if i != "ü" or "ğ" or "ç" or "ö" or "ş" or "ı":
            chars2.append(i)
    print(chars2)

Expected result: ['a','d']

Result: ['ü','ç','ö','a','ş','ğ','ı','d']

Upvotes: 0

Views: 54

Answers (2)

DocDriven
DocDriven

Reputation: 3974

For better maintainability you could put the chars you want to filter into a separate list, and do this:

chars = ["ü","ç","ö","a","ş","ğ","ı","d"]
filter = ["ü","ç","ö","ş","ğ","ı"]

chars2 = [c for c in chars if c not in filter]

Upvotes: 1

Rangeesh
Rangeesh

Reputation: 363

kindly change it as follows for your code to work

    for i in chars:
        if i != "ü" and i!= "ğ" and i!="ç" and i!= "ö" and i!="ş" and i!="ı":
            chars2.append(i)
    print(chars2)

Upvotes: 0

Related Questions