Python Forum
unique (single) value in dict (or list)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unique (single) value in dict (or list)
#1
Given any random dictionary (or a list doesnt matter), how to ask the dictionary if there is an unique value (so which only occurs once).

{
"1": "a",
"2": "b",
"1": "c",
"1": "c",
"1": "33",
"1": "a"
}

--> No unique value is found ... or technically 2 (b and 33) are found, but I only want 1.
so not desired result ... therefor errorhandling or so.

{
"1": "a",
"2": "b",
"1": "c",
"1": "c",
"1": "c",
"1": "a"
}

--> Unique value is found (b) ... do something.

What would your approach be?
Set returns all unique values but I'm not after that.
Reply
#2
What have you tried?
Reply
#3
Still unsure about the approach ndc85430, but thinking about something like this.



mylist = [ [1, "a"], [2, "b"], [1, "c"], [1, "c"],  [1, "c"], [1, "a"] ]

test = [a[1] for a in mylist]
# ['a', 'b', 'c', 'c', '33', 'a']

from collections import Counter
collection_counter = Counter(test)
for m in mylist:
    print "element {} num of times: {}".format(m[0], collection_counter.get(m[1]))

num_occurance = 0
newlist = ""
for key in collection_counter: 
    if collection_counter[key] == 1: 
        num_occurance = num_occurance + 1
        newlist = key
print num_occurance

if num_occurance == 1:
    print "unique value found: {}".format(newlist)
    print [x for x in mylist if x[1] == newlist]
else:
    print "bummer, no uniques found"
        
    
mylist = [ [1, "a"], [2, "b"], [1, "c"], [1, "c"],  [1, "c"], [1, "a"] ]
#[[2, 'b']]
#(annoying nested list ..grrr)

mylist = [ [1, "a"], [2, "b"], [1, "c"], [1, "c"],  [1, "33"], [1, "a"] ]
# bummer, no uniques found 
#(annoying nested list ..grrr)
Reply
#4
Like this?
items1 = {1:1, 2:2, 3:3}
items2 = {1:1, 2:1, 3:2, 4:2, 5:3}
items3 = {1:1, 2:1, 3:3, 4:5, 5:4}

def find_unique(d):
    value_list = list(d.values())
    value_set = set(value_list)
    if len(value_set) == len(value_list):
        return None # All values are unique

    unique = None
    for value in value_set:
        if value_list.count(value) == 1:
            if unique is None:
                unique = value
            else:
                return None # Already found a unique value
    return unique

print(items1, '  Unique =', find_unique(items1))
print(items2, '  Unique =', find_unique(items2))
print(items3, '  Unique =', find_unique(items3))
Output:
{1: 1, 2: 2, 3: 3} Unique = None {1: 1, 2: 1, 3: 2, 4: 2, 5: 3} Unique = 3 {1: 1, 2: 1, 3: 3, 4: 5, 5: 4} Unique = None
Reply
#5
yes. that looks like what I want to achieve.

your approach looks more logical than mine, thanks!
Reply
#6
on second thoughts: how to return the dictionary item (key+val)?

I think that dictionary isn't the logical approach for this?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sample random, unique string pairs from a list without repetitions walterwhite 1 402 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,162 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  Are list/dict comprehensions interpreted really sequentially? anata2047 3 1,415 May-31-2022, 08:43 PM
Last Post: Gribouillis
  How to get unique entries in a list and the count of occurrence james2009 5 2,915 May-08-2022, 04:34 AM
Last Post: ndc85430
  Updating nested dict list keys tbaror 2 1,243 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Parse String between 2 Delimiters and add as single list items lastyle 5 3,289 Apr-11-2021, 11:03 PM
Last Post: lastyle
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,260 Jan-30-2021, 07:11 AM
Last Post: alloydog
  convert List with dictionaries to a single dictionary iamaghost 3 2,805 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,281 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
Question dict value, how to change type from int to list? swissjoker 3 2,697 Dec-09-2020, 09:50 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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