Python Forum
How to discard list repeat values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to discard list repeat values
#7
# use set to get rid of duplicates like this

numbers = [1, 2, 3, 1, 2, 3, 4, 1]
unique_numbers = set(numbers)

# this works great: splat it!

numbers = [1, 2, 3, 1, 2, 3, 4, 1]
unique_numbers2 = {*numbers}

# you can't do this, because a list can't be a member of a set

numbers = [1, 2, 3, 1, 2, 3, 4, 1]
unique_numbers = {numbers}

# to your problem

mylist = [['seg', 'João', 3585], ['qui', 'João', 3847], ['seg', 'João', 3847], ['qua', 'João', 3913], ['qua', 'João', 3585]]

# an empty set

no_repeats = set()

# add the contents of the sub-lists to no_repeats

for item in mylist:
    aset = set(item)
    no_repeats.update(aset)

# this returns: {3585, 3847, 3913, 'seg', 'qui', 'João', 'qua'}
# if you need a list: result = list(no_repeats)
# if you need a dictionary from this list, check out your last post
Reply


Messages In This Thread
How to discard list repeat values - by akanowhere - Dec-27-2020, 08:08 PM
RE: How to discard list repeat values - by buran - Dec-27-2020, 08:17 PM
RE: How to discard list repeat values - by buran - Dec-27-2020, 08:27 PM
RE: How to discard list repeat values - by Pedroski55 - Dec-28-2020, 09:47 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Copying the order of another list with identical values gohanhango 7 1,067 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Comparing List values to get indexes Edward_ 7 1,083 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,259 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Adding values with reduce() function from the list of tuples kinimod 10 2,518 Jan-24-2023, 08:22 AM
Last Post: perfringo
  user input values into list of lists tauros73 3 1,025 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  Repeat request by else stsxbel 2 1,150 Jul-30-2022, 03:34 PM
Last Post: stsxbel
  get out of while loop and stop repeat Frankduc 11 2,866 Apr-26-2022, 10:09 PM
Last Post: deanhystad
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 14,782 Jan-19-2022, 08:33 AM
Last Post: menator01
  Avoid multiple repeat in indent Frankduc 8 2,793 Jan-18-2022, 05:46 PM
Last Post: Frankduc

Forum Jump:

User Panel Messages

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