![]() |
How to discard list repeat values - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to discard list repeat values (/thread-31685.html) |
How to discard list repeat values - akanowhere - Dec-27-2020 Dears; In the bellow list there repeats values in the last indice: I need discard the the key and the value (all) if the value is repeated: Exemple: list[0][2] is 3585 and list[4][2] are the same!!! list = [['seg', 'João', 3585], ['qui', 'João', 3847], ['seg', 'João', 3847], ['qua', 'João', 3913], ['qua', 'João', 3585]] RE: How to discard list repeat values - buran - Dec-27-2020 Let's make the terminology right. There are no repeating elements in this list. It's a list of lists. There are repeating elements in the list elements (i.e. in the sublists). Then this is not a mapping like dict. There are no key value pairs. Finally - show what expected output is and what have you tried so far. RE: How to discard list repeat values - akanowhere - Dec-27-2020 (Dec-27-2020, 08:17 PM)buran Wrote: Let's make the terminology right. Discar the repeated element in sub list[4] 1 list = [['seg', 'João', 3585], ['qui', 'João', 3847], ['seg', 'João', 3847], ['qua', 'João', 3913], ['qua', 'João', 3585]]1
RE: How to discard list repeat values - buran - Dec-27-2020 it's still unclear why do you remove the last element (presumably from your first post - because of repeating 3585) and yet preserve repeating 3847 RE: How to discard list repeat values - akanowhere - Dec-27-2020 (Dec-27-2020, 08:27 PM)buran Wrote: it's still unclear sorry..... it´s will be removed too..but lets keep one repeat..... list = [['seg', 'João', 3585], ['qui', 'João', 3850], ['seg', 'João', 3847], ['qua', 'João', 3913], ['qua', 'João', 3585]]
RE: How to discard list repeat values - perfringo - Dec-28-2020 Don't use list as name. >>> list('abc') ['a', 'b', 'c'] >>> list = ['a', 'b', 'c'] >>> list('abc') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'list' object is not callable >>> list ['a', 'b', 'c'] RE: How to discard list repeat values - Pedroski55 - Dec-28-2020 # 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 RE: How to discard list repeat values - akanowhere - Dec-28-2020 (Dec-28-2020, 04:56 AM)perfringo Wrote: Don't use list as name. Tks a lot i was wrote lista...it was on portugues,,,when I tranlate I forgot that list is a reserved word.... |