Removing multiple the same items from a list

Hi there! I am a total beginner. Mosh mentions we can remove multiple the same items from a list.
I was wondering how. I’ve tried several ways, but failed.

letters = [‘a’, ‘b’, ‘c’, ‘b’, ‘d’, ‘b’]

Id like to remove 'b’s

Appreciate your help.

One way is to create a new list without the b’s.

letters = ['a', 'b', 'c', 'b', 'd', 'b']
filtered = []
for letter in letters:
  if letter != 'b':
    filtered.append(letter)
print(filtered)

A more declarative approach (but maybe harder to understand) would be to use the filter function.

letters = ['a', 'b', 'c', 'b', 'd', 'b']
filtered = list(filter(lambda letter: letter != 'b', letters))
print(filtered)

Appreciate it, eelsholz!

I think I initially asked the wrong question. I just wanted to keep one ‘b’. Maybe you could help me out?

Thanks

Got it finally:

letters = ['a', 'c', 'b', 'b', 'd']

filtered = []

for letter in letters:
    if letter not in filtered:

        filtered.append(letter)

print(filtered)

As just another way to go for it. I found this when someone had asked a similar question. Although, this is removing all bs, not leaving any.

Ooh I like that. Here’s a variation on it.

li = [1,'b',2,'b',3,'b',4]
print([x for x in li if x != 'b'])

Looks like I added extra steps and could have gone straight to the print statement instead of defining the function.