# Removing multiple the same items from a list

**URL:** <https://forum.codewithmosh.com/t/removing-multiple-the-same-items-from-a-list/9970>\
**Category:** Python\
**Created:** [January 15, 2022, 3:02am UTC](https://forum.codewithmosh.com/t/removing-multiple-the-same-items-from-a-list/9970 "2022-01-15T03:02:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![uLN](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uln/32/3084_2.png) [@uLN](https://forum.codewithmosh.com/u/uLN)\
**Post date:** [January 15, 2022, 3:02am UTC](https://forum.codewithmosh.com/t/removing-multiple-the-same-items-from-a-list/9970/1 "2022-01-15T03:02:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![eelsholz](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@eelsholz](https://forum.codewithmosh.com/u/eelsholz)\
**Post date:** [January 25, 2022, 1:37am UTC](https://forum.codewithmosh.com/t/removing-multiple-the-same-items-from-a-list/9970/2 "2022-01-25T01:37:42Z")

</div>

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

```auto
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.

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

```

---

<div class="post-metadata">

**Author:** ![uLN](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uln/32/3084_2.png) [@uLN](https://forum.codewithmosh.com/u/uLN)\
**Post date:** [January 26, 2022, 7:47am UTC](https://forum.codewithmosh.com/t/removing-multiple-the-same-items-from-a-list/9970/3 "2022-01-26T07:47:23Z")

</div>

Appreciate it, eelsholz!

---

<div class="post-metadata">

**Author:** ![uLN](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uln/32/3084_2.png) [@uLN](https://forum.codewithmosh.com/u/uLN)\
**Post date:** [January 26, 2022, 2:02pm UTC](https://forum.codewithmosh.com/t/removing-multiple-the-same-items-from-a-list/9970/4 "2022-01-26T14:02:59Z")

</div>

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

Thanks

---

<div class="post-metadata">

**Author:** ![uLN](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uln/32/3084_2.png) [@uLN](https://forum.codewithmosh.com/u/uLN)\
**Post date:** [January 26, 2022, 2:19pm UTC](https://forum.codewithmosh.com/t/removing-multiple-the-same-items-from-a-list/9970/5 "2022-01-26T14:19:30Z")

</div>

Got it finally:

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

filtered = []

for letter in letters:
    if letter not in filtered:

        filtered.append(letter)

print(filtered)

```

---

<div class="post-metadata">

**Author:** ![Andy30](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@Andy30](https://forum.codewithmosh.com/u/Andy30)\
**Post date:** [January 27, 2022, 8:04pm UTC](https://forum.codewithmosh.com/t/removing-multiple-the-same-items-from-a-list/9970/6 "2022-01-27T20:04:01Z")

</div>

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.

 ![Screenshot_20220118-214809_Pydroid 3](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/2/25300e8c696b226e81907fcda05ac8e82ca0290e.jpeg)

---

<div class="post-metadata">

**Author:** ![eelsholz](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@eelsholz](https://forum.codewithmosh.com/u/eelsholz)\
**Post date:** [January 28, 2022, 1:22am UTC](https://forum.codewithmosh.com/t/removing-multiple-the-same-items-from-a-list/9970/7 "2022-01-28T01:22:48Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Andy30](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@Andy30](https://forum.codewithmosh.com/u/Andy30)\
**Post date:** [January 28, 2022, 3:44am UTC](https://forum.codewithmosh.com/t/removing-multiple-the-same-items-from-a-list/9970/8 "2022-01-28T03:44:36Z")

</div>

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