How to filter a dictionary by value?

Newbie question here, so please bear with me.

Let's say I have a dictionary looking like this:

a = {"2323232838": ("first/dir", "hello.txt"),
     "2323221383": ("second/dir", "foo.txt"),
     "3434221": ("first/dir", "hello.txt"),
     "32232334": ("first/dir", "hello.txt"),
     "324234324": ("third/dir", "dog.txt")}

I want all values that are equal to each other to be moved into another dictionary.

matched = {"2323232838": ("first/dir", "hello.txt"),
           "3434221":    ("first/dir", "hello.txt"),
           "32232334":   ("first/dir", "hello.txt")}

And the remaining unmatched items should be looking like this:

remainder = {"2323221383": ("second/dir", "foo.txt"),
             "324234324":  ("third/dir", "dog.txt")}

Thanks in advance, and if you provide an example, please comment it as much as possible.


Asked by: Ryan123 | Posted: 30-11-2021






Answer 1

The code below will result in two variables, matches and remainders. matches is an array of dictionaries, in which matching items from the original dictionary will have a corresponding element. remainder will contain, as in your example, a dictionary containing all the unmatched items.

Note that in your example, there is only one set of matching values: ('first/dir', 'hello.txt'). If there were more than one set, each would have a corresponding entry in matches.

import itertools

# Original dict
a = {"2323232838": ("first/dir", "hello.txt"),
     "2323221383": ("second/dir", "foo.txt"),
     "3434221": ("first/dir", "hello.txt"),
     "32232334": ("first/dir", "hello.txt"),
     "324234324": ("third/dir", "dog.txt")}

# Convert dict to sorted list of items
a = sorted(a.items(), key=lambda x:x[1])

# Group by value of tuple
groups = itertools.groupby(a, key=lambda x:x[1])

# Pull out matching groups of items, and combine items   
# with no matches back into a single dictionary
remainder = []
matched   = []

for key, group in groups:
   group = list(group)
   if len(group) == 1:
      remainder.append( group[0] )
   else:
      matched.append( dict(group) )
else:
   remainder = dict(remainder)

Output:

>>> matched
[
  {
    '3434221':    ('first/dir', 'hello.txt'), 
    '2323232838': ('first/dir', 'hello.txt'), 
    '32232334':   ('first/dir', 'hello.txt')
  }
]

>>> remainder
{
  '2323221383': ('second/dir', 'foo.txt'), 
  '324234324':  ('third/dir', 'dog.txt')
}

As a newbie, you're probably being introduced to a few unfamiliar concepts in the code above. Here are some links:

Answered by: Miranda648 | Posted: 01-01-2022



Answer 2

What you're asking for is called an "Inverted Index" -- the distinct items are recorded just once with a list of keys.

>>> from collections import defaultdict
>>> a = {"2323232838": ("first/dir", "hello.txt"),
...      "2323221383": ("second/dir", "foo.txt"),
...      "3434221": ("first/dir", "hello.txt"),
...      "32232334": ("first/dir", "hello.txt"),
...      "324234324": ("third/dir", "dog.txt")}
>>> invert = defaultdict( list )
>>> for key, value in a.items():
...     invert[value].append( key )
... 
>>> invert
defaultdict(<type 'list'>, {('first/dir', 'hello.txt'): ['3434221', '2323232838', '32232334'], ('second/dir', 'foo.txt'): ['2323221383'], ('third/dir', 'dog.txt'): ['324234324']})

The inverted dictionary has the original values associated with a list of 1 or more keys.

Now, to get your revised dictionaries from this.

Filtering:

>>> [ invert[multi] for multi in invert if len(invert[multi]) > 1 ]
[['3434221', '2323232838', '32232334']]
>>> [ invert[uni] for uni in invert if len(invert[uni]) == 1 ]
[['2323221383'], ['324234324']]

Expanding

>>> [ (i,multi) for multi in invert if len(invert[multi]) > 1 for i in invert[multi] ]
[('3434221', ('first/dir', 'hello.txt')), ('2323232838', ('first/dir', 'hello.txt')), ('32232334', ('first/dir', 'hello.txt'))]
>>> dict( (i,multi) for multi in invert if len(invert[multi]) > 1 for i in invert[multi] )
{'3434221': ('first/dir', 'hello.txt'), '2323232838': ('first/dir', 'hello.txt'), '32232334': ('first/dir', 'hello.txt')}

A similar (but simpler) treatment works for the items which occur once.

Answered by: Clark992 | Posted: 01-01-2022



Answer 3

I assume that your unique id will be the key.
Probably not very beautiful, but returns a dict with your unique values:

>>> dict_ = {'1': ['first/dir', 'hello.txt'],
'3': ['first/dir', 'foo.txt'], 
'2': ['second/dir', 'foo.txt'], 
'4': ['second/dir', 'foo.txt']}  
>>> dict((v[0]+v[1],k) for k,v in dict_.iteritems())  
{'second/dir/foo.txt': '4', 'first/dir/hello.txt': '1', 'first/dir/foo.txt': '3'}  

I've seen you updated your post:

>>> a
{'324234324': ('third/dir', 'dog.txt'), 
'2323221383': ('second/dir', 'foo.txt'), 
'3434221': ('first/dir', 'hello.txt'), 
'2323232838': ('first/dir', 'hello.txt'), 
'32232334': ('first/dir', 'hello.txt')}
>>> dict((v[0]+"/"+v[1],k) for k,v in a.iteritems())
{'second/dir/foo.txt': '2323221383', 
'first/dir/hello.txt': '32232334', 
'third/dir/dog.txt': '324234324'}

Answered by: Elise126 | Posted: 01-01-2022



Answer 4

Iterating over a dictionary is no different from iterating over a list in python:

for key in dic:
    print("dic[%s] = %s" % (key, dic[key]))

This will print all of the keys and values of your dictionary.

Answered by: Melissa228 | Posted: 01-01-2022



Answer 5

if you know what value you want to filter out:

known_tuple = 'first/dir','hello.txt'
b = {k:v for k, v in a.items() if v == known_tuple}

then a would become:

a = dict(a.items() - b.items())

this is py3k notation, but I'm sure something similar can be implemented in legacy versions. If you don't know what the known_tuple is, then you'd need to first find it out. for example like this:

c = list(a.values())
for i in set(c):
    c.remove(i)
known_tuple = c[0]

Answered by: Leonardo877 | Posted: 01-01-2022



Similar questions

Python : filter list items from list of dictionary

names = ['vapp1', 'vapp3', 'vapp4', 'vapp2'] vapps = [{'name':'vapp2', 'ip': '11.21.18.24', 'obj': 'obj523'}, {'name':'vapp3', 'ip': '11.21.18.27', 'obj': 'obj234'}, {'name':'vapp5', 'ip': '11.21.18.25', 'obj': 'obj246'}] result = [vapp for vapp in vapps if vapp['name'] in names] print result Using this list/dict comprehension I am getting what I want in result. But I also want to print that


python - Filter values of dictionary

This question already has answers here:


python - How can I filter a key out of a dictionary based on the value

I have this dict : mydict = {'Andreas': 0.4833775399766172, 'Anh': nan, 'Maryse': 0.61436719272499474, 'Darren': -0.44898819782452443, 'Jesse': 0.14565852997686479, 'Mitchell': nan} The nan's give me no information at all so I want to filter them out. for k, v in mydict.iteritems(): if v == 'nan': del mydict[v] I tried this, but it doesn't wor...


python - Filter Dictionary

I have been trying to filter a dictionary to find which statements in the dictionary fit and then print them. The idea is that all statements where the cost of product is greater than money paid is being printed. I do not know how to filter those out. Below is the code being used: data = open("tableQuotes.txt","r") for line in data: line = line.strip() (quote, customer, amount, paid) = line.split(",...


python - filter the table by a search query with search fields is in a dictionary

I have a dictionary &amp; a string value like: d= {"check-in": "12-04-2016", "check-out": "13-04-2016", "location": "India"} value = 'india' search = d['check-in'] Here I am reading the keys which are a column name in my DB table. Note: Here everything is dynamic that means value of columns are dynamically set. I want to query with multiple filter in django such a way ...


python - Dictionary Containing list data, filter based on value in list

I have test data which is gathered based on multiple inputs, and results in a single output. I'm currently storing this data in a dictionary whose keys are my parameter/ results labels, and whose values are the test conditions and results. I would like to be able to filter the data so I can generate plots based on isolated conditions. In my example below, my test conditions would be 'a' and 'b', and the result ...


python - Filter out key from dictionary

Having dictionary (defined in map.jinja) {% set intellij = salt['grains.filter_by']({ 'default': { 'orig_name': 'idea-IC-145.1617.8', 'download_url': 'www.example.com', 'archive_format': 'tar', 'archive_opts': 'xfz', 'owner': 'root', 'owner_link_location': '/blabla/bin/idea', }, }, merge=salt['pillar.get']('intellij')) %}


python - Trying to filter a dictionary for an AI

Hi so I'm currently taking a class and one of our assignments is to create a Hangman AI. There are two parts to this assignment and currently I am stuck on the first task, which is, given the state of the hangman puzzle, and a list of words as a dictionary, filter out non-possible answers to the puzzle from the dictionary. As an example, if the puzzle given is t--t, then we should filter out all words that are not 4 letter...


python - Filter a dictionary if value of a key is true

I am totally new to Python (day 1). I have a dataset that indicates whether someone is a person of interest via a boolean 'poi' key. I was able to filter the data with the following: filtered = [] for n in enron_data: if enron_data[n]['poi']: filtered.append(enron_data[n]); print(len(filtered)) I tried for a while to use pythons built in filter but was unable to. what is a clean way to...


python - CSV Dictionary to filter a CSV file

I am using Python 3.6 with pandas and numpy. I have two CSV files, both not containing any titles (so indexing is builtin). One is a one column list with computernames: PC001 PC002 PC003 ... The other file is an import-file for a system. It is a csv-file. And the pc name is the third column: addprinter,terminal,PC001,something,something addprinter,terminal,PC002,something,something addprinter,terminal,PC003,someth...


python - Filter dictionary list by text in string

I have a list of dictionaries and need to filter them by a list of strings. Right now I have this: projects = [{u'CustomerName': u'abc', u'Projectname': u'Contract A full'}, {u'CustomerName': u'bcd', u'Projectname': u'Contract A medium'}, {u'CustomerName': u'cde', u'Projectname': u'Contract B full'}, {u'CustomerName': u'def', u'Projectname': u'Contract B medium'}] filter = ['B', 'full'] return [p...


python - Filter a dictionary based on values stored in an array

I have a dictionary of the following format: data={'city':np.array(['chicago','champaign','kansas city','dallas','champaign','chicago']), 'state':np.array(['IL','IL','KA','TX','IL','IL']), 'transaction_dt':np.array(['1-1-2018','2-1-2018','3-1-2018','7-1-2018','1-1-2018','2-1-2018'])} I want to get the transaction_dt for all transactions that happened in the state IL. How can I do this in...


python - How to filter dictionary by value?

This question already has answers here:


python - Filter two lists of dictionary by 2 value pairs AND group them together

I have 2 lists of dictionaries , let's say : List_D1 = [{'Symbol':'GFX','Time':'9:36am', 'Change':-0.18, 'Volume':181800}, {'Symbol':'AIG','Time':'9:36am', 'Change':-0.15, 'Volume': 195500}, {'Symbol':'AXP','Time':'9:36am', 'Change':-0.46, 'Volume': 935000}, ] List_D2 = [{'Symbol':'AA','Time':'7:36am', 'Change':-0.08, 'Volume':181800}, {'Symbol':'AIG','Time':'...


python - Filter dictionary and remove lowest values

I have dictionary as below. Is there a way to output a dictionary with the 5 highest values? If there are ties for the 5th highest value, I need to include those keys. Input dictionary: { "1": 1, "12": 1, "13":2, "3": 5, "5":8, "7":3, "4":8, "10":7 } Desired result: { "3": 5, "5":8, ...


python - Filter dictionary in a list

I need to filter a dictionary within a list. My data looks like this: [('John', 'Samantha', {'source': 'family'}), ('John', 'Jill', {'source': 'work'})] I need to filter records with source=family, I tried the following but didn't work: expectedResult = [i for i in my_list if i['source'] == 'family'] Many thanks for ...


dictionary - python filter a list of dict to return a single dict

I have a list of dicts and need to filter on the 'name' key (values are guaranteed to be unique) in the nested dict in order to return a single dict. I have a working solution but thought there would be more efficient/elegant/pythonic methods. I tried dictionary comprehension but couldn't get my head around how it would iterate over the list Solutions for for 2.7+ and 3 would be appreciated.


python - Filter a dictionary based on the value of its date keys

I want to import articles from as many sources around the world as from a certain date. import requests url = ('https://newsapi.org/v2/top-headlines?' 'country=us&amp;' 'apiKey=de9e19b7547e44c4983ad761c104278f') response = requests.get(url) response_dataframe = pd.DataFrame(response.json()) articles = {article for article in response_dataframe['articles'] if article['publishedAt'] &gt;= '201...


python - Using Jinja to filter by nested dictionary values

I have the following YAML in my SaltStack Pillar: prometheus: services: cassandra: enabled: False cockroachdb: enabled: True haproxy: enabled: True swift: enabled: False I want to be able to loop over a list of enabled services. {% for enabled_service_name in prometheus.services | selectattr('enabled') %} {{ enabled_service_name }} ...


python - filter a list of dictionary based on two keys

with open('test.csv') as f: list_of_dicts = [{k:v for k, v in row.items()} for row in csv.DictReader(f, skipinitialspace=True)] Hello,I have csv file which I make to a list of dictionaries,I want to filter its output on ASIN (remove duplicate if found)based on "Merchant 1 Price" I want to get the lower price, not all of them have duplicates i.e remove duplicates (keep the one with the lowest m...


python - List all words in a dictionary that start with <user input>

How would a go about making a program where the user enters a string, and the program generates a list of words beginning with that string? Ex: User: "abd" Program:abdicate, abdomen, abduct... Thanks! Edit: I'm using python, but I assume that this is a fairly language-independent problem.


python, dictionary and int error

I have a very frustrating python problem. In this code fixedKeyStringInAVar = "SomeKey" def myFunc(a, b): global sleepTime global fixedKeyStringInAVar varMe=int("15") sleepTime[fixedKeyStringInAVar] = varMe*60*1000 #more code Now this works. BUT sometimes when I run this function I get TypeError: 'int' object does not support item assignment


python - Best way to create a NumPy array from a dictionary?

I'm just starting with NumPy so I may be missing some core concepts... What's the best way to create a NumPy array from a dictionary whose values are lists? Something like this: d = { 1: [10,20,30] , 2: [50,60], 3: [100,200,300,400,500] } Should turn into something like: data = [ [10,20,30,?,?], [50,60,?,?,?], [100,200,300,400,500] ] ...


python - List a dictionary

In a list appending is possible. But how I achieve appending in dictionary? Symbols from __ctype_tab.o: Name Value Class Type Size Line Section __ctype |00000000| D | OBJECT|00000004| |.data __ctype_tab |00000000| r | OBJECT|00000101| |.rodata Symbols from _ashldi3.o: Name Value Class ...


Python and dictionary like object

I need a python 3.1 deep update function for dictionaries (a function that will recursively update child dictionaries that are inside a parent dictionary). But I think, in the future, my function could have to deal with objects that behave like dictionaries but aren't. And furthermore I want to avoid using isinstance and type (because they are considered b...


python - Remove dictionary from list

If I have a list of dictionaries, say: [{'id': 1, 'name': 'paul'}, {'id': 2, 'name': 'john'}] and I would like to remove the dictionary with id of 2 (or name 'john'), what is the most efficient way to go about this programmatically (that is to say, I don't know the index of the entry in the list so it can't simply be popped).


C# way to mimic Python Dictionary Syntax

Is there a good way in C# to mimic the following python syntax: mydict = {} mydict["bc"] = {} mydict["bc"]["de"] = "123"; # &lt;-- This line mydict["te"] = "5"; # &lt;-- While also allowing this line In other words, I'd like something with [] style access that can return either another dictionary or a string type, depending on how it has been set. I've been trying to work...


python - Can a dictionary be passed to django models on create?

Is it possible to do something similar to this with a list, dictionary or something else? data_dict = { 'title' : 'awesome title', 'body' : 'great body of text', } Model.objects.create(data_dict) Even better if I can extend it: Model.objects.create(data_dict, extra='hello', extra2='world')


python - Make Dictionary From 2 List

This question already has answers here:


Python dictionary simple way to add a new key value pair

Say you have, foo = 'bar' d = {'a-key':'a-value'} And you want d = {'a-key':'a-value','foo':'bar'} e = {'foo':foo} I know you can do, d['foo'] = foo #Either of the following for e e = {'foo':foo} e = dict(foo=foo) But, in all these way to add the variable foo to dict, I have had to use the word foo twice; onc...


sorting - In Python, how can you easily retrieve sorted items from a dictionary?

Dictionaries unlike lists are not ordered (and do not have the 'sort' attribute). Therefore, you can not rely on getting the items in the same order when first added. What is the easiest way to loop through a dictionary containing strings as the key value and retrieving them in ascending order by key? For example, you had this: d = {'b' : 'this is b', 'a': 'this is a' , 'c' : 'this is c'}


Python dictionary from an object's fields

Do you know if there is a built-in function to build a dictionary from an arbitrary object? I'd like to do something like this: &gt;&gt;&gt; class Foo: ... bar = 'hello' ... baz = 'world' ... &gt;&gt;&gt; f = Foo() &gt;&gt;&gt; props(f) { 'bar' : 'hello', 'baz' : 'world' } NOTE: It should not include methods. Only fields.


python - How do you retrieve items from a dictionary in the order that they're inserted?

Is it possible to retrieve items from a Python dictionary in the order that they were inserted?


python - How can I make a dictionary from separate lists of keys and values?

I want to combine these: keys = ['name', 'age', 'food'] values = ['Monty', 42, 'spam'] Into a single dictionary: {'name': 'Monty', 'age': 42, 'food': 'spam'}


python - Dictionary or If statements, Jython

I am writing a script at the moment that will grab certain information from HTML using dom4j. Since Python/Jython does not have a native switch statement I decided to use a whole bunch of if statements that call the appropriate method, like below: if type == 'extractTitle': extractTitle(dom) if type == 'extractMetaTags': extractMetaTags(dom)


Is a Python dictionary an example of a hash table?

One of the basic data structures in Python is the dictionary, which allows one to record "keys" for looking up "values" of any type. Is this implemented internally as a hash table? If not, what is it?


python - Is there a "one-liner" way to get a list of keys from a dictionary in sorted order?

The list sort() method is a modifier function that returns None. So if I want to iterate through all of the keys in a dictionary I cannot do: for k in somedictionary.keys().sort(): dosomething() Instead, I must: keys = somedictionary.keys() keys.sort() for k in keys: dosomething() Is there a pretty way to iterate t...


python - Interface to versioned dictionary

I have an versioned document store which I want to access through an dict like interface. Common usage is to access the latest revision (get, set, del), but one should be able to access specific revisions too (keys are always str/unicode or int). from UserDict import DictMixin class VDict(DictMixin): def __getitem__(self, key): if isinstance(key, tuple): docid, rev = key e...


python - List all words in a dictionary that start with <user input>

How would a go about making a program where the user enters a string, and the program generates a list of words beginning with that string? Ex: User: "abd" Program:abdicate, abdomen, abduct... Thanks! Edit: I'm using python, but I assume that this is a fairly language-independent problem.


python - Check if a given key already exists in a dictionary and increment it

How do I find out if a key in a dictionary has already been set to a non-None value? I want to increment the value if there's already one there, or set it to 1 otherwise: my_dict = {} if my_dict[key] is not None: my_dict[key] = 1 else: my_dict[key] += 1






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top