Python Forum
How to remove multiples in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to remove multiples in a list
#1
How can remove something in a list if it appears more than once?
examples:

list1 = [1, 2, 3, 4, 1]
>>>>>
list1 = [1, 2, 3, 4]


or


list2 = ['a', 'b', 'a', 'a']
>>>>>
list2 = ['a', 'b']
Reply
#2
What have you tried? Is order important or not?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
I have tried the following:
old_list = ['a', 'b', 'a', 'a', 'b']
new_list = []
for i in range(len(old_list)):
    if old_list[i] not in new_list:   
        new_list.append(old_list[i])
print(new_list)
>>>>>
#This prints: ['a', 'b']
This works ofcourse, but i was wondering if there was an easier way of doing this.
Or atleast something less than 5 lines of code in order to make my code look a little bit cleaner.
I was thinking that maybe there was a command for it like

list1 = ['a', 'b', 'a', 'a', 'b']
remove_multiples(list1)
print(list1)
>>>>>
#So it will print:['a', 'b']
Order is not important.
Reply
#4
If order is not important, you can use a set. A set can only have one of each value, so it can very efficiently strip out duplicates:

list1 = ['a', 'b', 'a', 'a', 'b']
uniques = set(list1)               # set(['a', 'b'])
uniques2 = list(set(list1))        # ['a', 'b']
Order is generally lost when you convert to a set, however.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Thanks!
Reply
#6
jasper100125 Wrote:This works ofcourse, but i was wondering if there was an easier way of doing this.
If you don't mind using a third party library, there is
from more_itertools import unique_everseen
new_list = list(unique_everseen(old_list))
Reply
#7
if order is important, in 3.7+ you can use order preserving feature of dicts
>>> spam = ['a', 'b', 'a', 'c', 'b', 'a']
>>> eggs = list(dict(zip(spam, spam)).keys())
>>> eggs
['a', 'b', 'c']
>>>
before 3.7 you need to use collections.OrderedDict instead.

also it's worth mentioning that this solution as well using set() will work only if list elements are hashable.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 423 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,320 Nov-13-2022, 01:27 AM
Last Post: menator01
  Remove empty keys in a python list python_student 7 3,012 Jan-12-2022, 10:23 PM
Last Post: python_student
  Remove an item from a list contained in another item in python CompleteNewb 19 5,665 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  .remove() from a list - request for explanation InputOutput007 3 2,212 Jan-28-2021, 04:21 PM
Last Post: InputOutput007
  I want to create small multiples grouped bar plot, and facing some aesthetics issuess dev_kaur 0 1,610 Dec-05-2020, 08:49 AM
Last Post: dev_kaur
  Remove double quotes from the list ? PythonDev 22 8,751 Nov-05-2020, 04:53 PM
Last Post: snippsat
  Remove specific elements from list with a pattern Xalagy 3 2,690 Oct-11-2020, 07:18 AM
Last Post: Xalagy
  How to remove dict from a list? Denial 7 2,922 Sep-28-2020, 02:40 PM
Last Post: perfringo
  Read Multiples Text Files get specific lines based criteria zinho 5 3,110 May-19-2020, 12:30 PM
Last Post: zinho

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020