Python Forum
how to take out duplicates from a list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to take out duplicates from a list?
#1
Hello! So my teacher wants up to take out duplicates from a list that the user puts. This is my code:

num=int(input("First List- How many elements do you want?: "))
lista=list()
for i in range(num):
    element=int(input("   Input a number: "))
    lista.append(element)

num=int(input("Second List- How many elements do you want?: "))
listb=list()
for i in range(num):
    element=int(input("   Input a number: "))
    listb.append(element)

print "This is your new list: "
listc=list()
listc=lista+listb
listc.sort()
print listc
So basically, if listc's output is
Output:
[2,2,3,5,5,8,9]
because the user may have put the same numbers twice, then we need to take out the duplicates so it then becomes this instead.
Output:
[2,3,5,8,9]


My teacher gave us this but I don't quite understand it, or how to even incorporate it into my code, and if anything I might not:
new=lista
for i in listc:
    m=listc[0]
    if m not in new:
        new.append(m)
Anyways, thank you if anyone answers, I appreciate it. Blush
Reply
#2
The simplest way.
>>> lst = [2,2,3,5,5,8,9]
>>> set(lst)
{2, 3, 5, 8, 9}
(Mar-08-2019, 01:55 PM)helpme Wrote: My teacher gave us this but I don't quite understand it, or how to even incorporate it into my code, and if anything I might not:
new=lista
for i in listc:
    m=listc[0]
    if m not in new:
        new.append(m)
It's written in a confusing and not so good way can you tell your teacher Wink
See if this is better to understand.
>>> lst = [2,2,3,5,5,8,9]
>>> unique = []
>>> for ele in lst:
...     if ele not in unique:
...         unique.append(ele)
...         
>>> unique
[2, 3, 5, 8, 9]
Reply
#3
Yeah, I don't understand that either. If you were looking to combine lista and listc without duplicates, it would be:

new=lista[:]
for i in listc:
    if i not in new:
        new.append(m)
So you loop through listc, and if it's not already in new, you add it to new. Note the [:] on the first line. That makes new a copy of lista, rather than just a reference to the same point in memory. Without that, any modifications to new also get made to lista. If you want lista to stay the same you need that. If you are fine with modifying lista, why make a new variable named 'new'?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,454 Sep-23-2022, 07:53 AM
Last Post: deanhystad
  help with adding duplicates elements together in a list 2ECC3O 5 2,015 Sep-10-2022, 07:11 AM
Last Post: 2ECC3O
  Dealing with duplicates to an Excel sheet DistraughtMuffin 6 3,258 Oct-28-2020, 05:16 PM
Last Post: Askic
  duplicates nsx200 3 2,420 Nov-12-2019, 08:55 AM
Last Post: nsx200
  Exercise list remove duplicates RavCOder 9 5,236 Oct-23-2019, 04:16 PM
Last Post: jefsummers
  How to keep duplicates and remove all other items in list? student8 1 4,937 Oct-28-2017, 05:52 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

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