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
#1
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]]
Reply
#2
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.
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
#3
(Dec-27-2020, 08:17 PM)buran Wrote: 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.

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
Output:
list = [['seg', 'João', 3585], ['qui', 'João', 3847], ['seg', 'João', 3847], ['qua', 'João', 3913]]
Reply
#4
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
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
#5
(Dec-27-2020, 08:27 PM)buran Wrote: 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

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]]
Output:
list = [['seg', 'João', 3585], ['qui', 'João', 3847], ['seg', 'João', 3847], ['qua', 'João', 3913]]
Reply
#6
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']
akanowhere likes this post
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
#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
#8
(Dec-28-2020, 04:56 AM)perfringo Wrote: 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']

Tks a lot i was wrote lista...it was on portugues,,,when I tranlate I forgot that list is a reserved word....
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass list of values to a API request URL chetansaip99 0 472 Sep-28-2021, 07:37 AM
Last Post: chetansaip99
  Python dictionary with values as list to CSV Sritej26 4 914 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 873 Nov-23-2020, 11:01 PM
Last Post: perfringo
  Trouble with converting list , dict to int values! faryad13 7 1,474 Sep-04-2020, 06:25 AM
Last Post: faryad13
  How to make a list of values from a dictionary list? faryad13 2 798 Sep-03-2020, 03:45 PM
Last Post: faryad13
  Print variable values from a list of variables xnightwingx 3 927 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Accessing values in list of dictionaries pythonnewbie138 2 752 Aug-02-2020, 05:02 PM
Last Post: pythonnewbie138
  Function to return list of all the INDEX values of a defined ndarray? pjfarley3 2 817 Jul-10-2020, 04:51 AM
Last Post: pjfarley3
  access dictionary with keys from another and write values to list redminote4dd 6 1,357 Jun-03-2020, 05:20 PM
Last Post: DeaD_EyE
  Grabbing comma separed values from SQLite and putting them in a list PythonNPC 8 1,579 Apr-10-2020, 02:39 PM
Last Post: buran

Forum Jump:

User Panel Messages

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