Remove all keys excepts those mentioned from a dict python3 - python-3.x

i have a nested python dictionary where i am trying to remove all keys except those mentioned. I found a solution here where we can remove keys specified, what i am trying to do is reverse of it, i want to remove all keys except those mentioned. So i made a slight change in the function, but i am getting an empty dict.
from collections import MutableMapping
def remove_fields(d, list_of_keys_to_remove):
if not isinstance(d, (dict, list)):
return d
if isinstance(d, list):
return [v for v in (remove_fields(v, list_of_keys_to_remove) for v in d) if v]
return {k: v for k, v in ((k, remove_fields(v, list_of_keys_to_remove)) for k, v in d.items()) if
k in list_of_keys_to_remove}
Adding sample input
EDIT: let suppose i want to keep RENEWAL_TYPE
{
'G_1': [
{
'ACCOUNTING_RULE_ID': '1',
'PAYMENT_TERM_ID': '4',
'RENEWAL_TYPE': 'RENEW'
},
{
'ACCOUNTING_RULE_ID': '2',
'PAYMENT_TERM_ID': '4',
'RENEWAL_TYPE': 'RENEW'
},
{
'ACCOUNTING_RULE_ID': '3',
'PAYMENT_TERM_ID': '4',
'RENEWAL_TYPE': 'DO_NOT_RENEW'
},
{
'ACCOUNTING_RULE_ID': '4',
'PAYMENT_TERM_ID': '4',
'RENEWAL_TYPE': 'RENEW'
}
]
}

You're looking for the intersection between the set of keys in the dict and the set of values in the list. i.e. all keys that exist in both sets.
Try this:
from collections import MutableMapping
d = { 'A': 1, 'B': 2, 'C': 3, 'D': 4 }
list_of_keys_to_keep = ['B', 'C', 'F']
def remove_fields(d, list_of_keys_to_keep):
return {key: value for key, value in d.items() if key in list_of_keys_to_keep}
print(remove_fields(d, list_of_keys_to_keep)) # -> {'B': 2, 'C': 3}
Edit: Updated name of list_of_keys_to_remove to list_of_keys_to_keep since this seems to be what it actually represents.
Edit2: Updated after Askers update. The following works for the sample provided:
from collections import MutableMapping
d = {
'G_1': [
{
'ACCOUNTING_RULE_ID': '1',
'PAYMENT_TERM_ID': '4',
'RENEWAL_TYPE': 'RENEW'
},
{
'ACCOUNTING_RULE_ID': '2',
'PAYMENT_TERM_ID': '4',
'RENEWAL_TYPE': 'RENEW'
},
{
'ACCOUNTING_RULE_ID': '3',
'PAYMENT_TERM_ID': '4',
'RENEWAL_TYPE': 'DO_NOT_RENEW'
},
{
'ACCOUNTING_RULE_ID': '4',
'PAYMENT_TERM_ID': '4',
'RENEWAL_TYPE': 'RENEW'
}
]
}
list_of_keys_to_keep = ['RENEWAL_TYPE']
def filter_dict(di, keys):
if not isinstance(di, (dict, list)):
return di
if isinstance(di, list):
return [filter_dict(value, keys) for value in di]
return {key: filter_dict(value, keys) for key, value in di.items() if key in keys or isinstance(value, (dict, list))}
print(filter_dict(d, list_of_keys_to_keep))
Output:
{'G_1': [{'RENEWAL_TYPE': 'RENEW'}, {'RENEWAL_TYPE': 'RENEW'}, {'RENEWAL_TYPE': 'DO_NOT_RENEW'}, {'RENEWAL_TYPE': 'RENEW'}]}

try this one:
dict = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}
keys_to_preserve = ["e", "b", "c"]
dict_final={k: v for k,v in dict.items() if k in keys_to_preserve}

def remove_fields(d, keys_to_preserve):
if isinstance(d, list):
return [remove_fields(v, keys_to_preserve) for v in d]
elif isinstance(d, dict):
return dict([(k, remove_fields(v, keys_to_preserve)) for k, v in d.items() if k in keys_to_preserve])
else:
return d

Related

Python3 for loop over a dict

I am trying to get all the values individually for each asset (BCHUSD and TRXUSD).
What I want to do is something like this:
BCHUSD a = 301.340000 b = 301.160000 c = 301.280000
TRXUSD a = 0.0609450 b = 0.0609440 c = 0.0609540
Could someone tell me how I can do it please?
Regards!
import requests
import json
while True:
req = requests.get('https://api.kraken.com/0/public/Ticker?pair=BCHUSD,TRXUSD,XRPUSD')
print(req)
<Response [200]>
print(type(req))
<class 'requests.models.Response'>
obj = req.json()
print(type(obj))
<class 'dict'>
for k, v in obj.items():
if type(v) is dict and k:
for nk, nv in v.items():
print(nk, nv)
BCHUSD {'a': ['298.240000', '11', '11.000'], 'b': ['298.040000', '3', '3.000'], 'c':
['299.000000', '0.89507885'], 'v': ['38.42175237', '5614.56089299'], 'p':
['300.890848', '277.650439'], 't': [24, 2314], 'l': ['299.000000', '260.000000'], 'h':
['302.390000', '309.900000'], 'o': '299.000000'}
TRXUSD {'a': ['0.0608250', '4881', '4881.000'], 'b': ['0.0607820', '40500',
'40500.000'], 'c': ['0.0608630', '81.94337742'], 'v': ['21067.61432979',
'9622286.56922629'], 'p': ['0.0610566', '0.0589675'], 't': [25, 1729], 'l':
['0.0608630', '0.0562060'], 'h': ['0.0612840', '0.0618410'], 'o': '0.0611130'}
XXRPZUSD {'a': ['0.69018000', '666', '666.000'], 'b': ['0.69000000', '42829',
'42829.000'], 'c': ['0.69022000', '358.00000000'], 'v': ['287549.02071579',
'27810492.67564827'], 'p': ['0.69737332', '0.65981291'], 't': [429, 10340], 'l':
['0.69000000', '0.62229000'], 'h': ['0.70386000', '0.72105000'], 'o': '0.69935000'}
I think the following could help you as a starting point:
response_json = {
"title": "name",
"abc": {'a': [1,2,3], "b": [2,3,4]},
"ohter_stuff": "xxx",
"xyz": {'a': [10, 20 ,30], "b": [20, 30, 40]}
}
# select relevant key value pairs
data = {
key: value for key, value in response_json.items()
if isinstance(value, dict)
}
# get the inner subdict index length
length = len(data['abc']['a'])
# get the inner subdict keys
items = list(data['abc'].keys())
# loop and print
for index in range(length):
for name, subdict in data.items():
# join the items at index pairs into a string
index_items = " ".join(
f"{item} = {subdict[item][index]}"
for item in items
)
print(name, index_items)
This is a pure standard library python solution. If you can install other libraries, I would recommend to have a look into pandas.

Problem when passing several lists in function with Python

here is my script :
time_remaining = [1, 2, 3, 4]
url_links = ["a", "b", "c"]
nb_bookies = ['5', '7', '6']
def gitanerie(time, url, book):
if len(time) != len(url):
url = url.append('d')
book = book.append('1')
else:
pass
return url, nb_bookies
url_links, nb_bookies = gitanerie(time_remaining, url_links, nb_bookies)
print(url_links)
print(nb_bookies)
When I run it it gives me :
None
['5', '7', '6', '1']
the url_kinks is empty. How to solve this problem. Thanks
Don't use in place url and book bacause append has no return type value rather follow this,
def gitanerie(time, url, book):
if len(time) != len(url):
url.append('d')
book.append('1')
else:
pass
return url, nb_bookies
You are returning the wrong variables in your function and instead of book = book.append('d') use book.append('d')
time_remaining = [1, 2, 3, 4]
url_links = ["a", "b", "c"]
nb_bookies = ['5', '7', '6']
def gitanerie(time, url, book):
if len(time) != len(url):
url.append('d')
book.append('1')
else:
pass
return url, book
url_links, nb_bookies = gitanerie(time_remaining, url_links, nb_bookies)
print(nb_bookies,url_links)
This will result in:
['5', '7', '6', '1'] ['a', 'b', 'c', 'd']

Is a dictionary within a dictionary with any list values

I have a question regarding dictionaries and how to tell if a dictionary appears in another. In the example below, I wish to check if d1 appears in d2 which as shown below:
d1 = { 'a': '1', 'b': '2', 'c': '3' }
d2 = { 'a': '1', 'b': '2', 'c': '3', 'd': '4', 'e': '5' }
res = all(item in d2.items() for item in d1.items())
However, I have a case where I wish to be able to pass in a list of values and check that the values for a, b and any of the values of c appear in d2.
d1 = { 'a': '1', 'b': '2', 'c': ['3', '33', '333'] }
d2 = { 'a': '1', 'b': '2', 'c': '3', 'd': '4', 'e': '5' }
Any help would be appreciated.
If I understand correctly, in your 2nd example, the result should be True since '3' belongs to list d1['c'] and d2['c'] == 3. If I am right then this might work:
d1 = { 'a': '1', 'b': '2', 'c': ['3', '33', '333'] }
d2 = { 'a': '1', 'b': '2', 'c': '3', 'd': '4', 'e': '5' }
res = all(item in d2.items() if not isinstance(item[1], list)
else any((item[0], x) in d2.items() for x in item[1])
for item in d1.items())
print(res)

Convert a list of strings to a list of strings and ints

I have a dictionary of lists, each list has 3 elements, i want to convert the 2nd and 3rd element to ints from strings
dict = {1:['string', '2', '3',],
2:['string', '2', '3',],
3:['string', '2', '3',],}
to become:
dict = {1:['string', 2, 3,],
2:['string', 2, 3,],
3:['string', 2, 3,],}
Thank you
Firstly, don't name your dictionaries dict, as it's a reserved keyword and you don't wanna overwrite it.
Coming to your solution.
d = {
1: ['string', '23', '3'],
2: ['string', '2', '3'],
3: ['string', '2', '3'],
}
d2 = {
k: [
int(i) if i.isdigit() else i
for i in v
]
for k, v in d.items()
}
Will give you an output of:
{
1: ['string', '23', '3'],
2: ['string', '2', '3'],
3: ['string', '2', '3']
}
{
1: ['string', 23, 3],
2: ['string', 2, 3],
3: ['string', 2, 3]
}
If you dictionary has many elements you might not want to create a second dictionary in memory, but modify the existing one in-place:
data = {
1: ['string', '2', '3'],
2: ['string', '2', '3'],
3: ['string', '2', '3'],
}
for v in data.values():
v.append(int(v.pop(1)))
v.append(int(v.pop(1)))
print(data)
Output:
{1: ['string', 2, 3], 2: ['string', 2, 3], 3: ['string', 2, 3]}

Making dictionary from 2 lists

I have following two lists:
list1 = ['17-Q2', '1.00', '17-Q3', '2.00', '17-Q4', '5.00', '18-Q1', '6.00']
list2 = ['17-Q2', '1', '17-Q3', '2', '17-Q4', '5', '18-Q1', '6']
I want a dictionary in the following way. Can I do that in Python?
result = [17-Q2: 1(1.00), 17-Q3: 2(2.00), 17-Q4: 5(5.00), 18-Q1: 6(6.00)]
Here's one approach to this:
result = {}
list1=['17-Q2', '1.00', '17-Q3', '2.00', '17-Q4', '5.00', '18-Q1', '6.00']
list2=['17-Q2', '1', '17-Q3', '2', '17-Q4', '5', '18-Q1', '6']
for i in range(0, len(list1)-1, 2):
result[list1[i]] = list2[i + 1] + '(' + list1[i+1] + ')' ;
You can zip the two lists and then zip the resulting iterable with itself so that you can iterate through it in pairs to construct the desired dict:
i = zip(list1, list2)
result = {k: '%s(%s)' % (v2, v1) for (k, _), (v1, v2) in zip(i, i)}
result becomes:
{'17-Q2': '1(1.00)', '17-Q3': '2(2.00)', '17-Q4': '5(5.00)', '18-Q1': '6(6.00)'}
You can use zip and dict.
keys = ["a", "b", "c", "d"]
values = ["A", "B", "C"]
print(dict(zip(keys, values)))
# prints: {'a': 'A', 'b': 'B', 'c': 'C'}
This works because dict can take a list (or any iterator) of tuples of (key, value) to be created. The zip function allows to build tuples from lists:
zip returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.
Notice that this will only return a dictionary that pairs the shortest list (either keys or value) with the corresponding element.
If you wish to have a default value for unpaired elements you can always use itertools.zip_longest
from itertools import zip_longest
keys = ["a", "b", "c", "d"]
values = ["A", "B", "C"]
print(dict(zip_longest(keys, values)))
# prints: {'a': 'A', 'b': 'B', 'c': 'C', 'd': None}
You can also use zip_longest keyword parameter fillvalue to have something else than None when the corresponding value isn't found.
On the other hand, if you have more values than keys, this wouldn't make much sense as you would erase the default key (namely fillvalue) for every missing element.
Assuming your input to be as follows:
list1 = ['17-Q2', '1.00', '17-Q3', '2.00', '17-Q4', '5.00', '18-Q1', '6.00']
list2 = ['17-Q2', '1', '17-Q3', '2', '17-Q4', '5', '18-Q1', '6']
And Desired Output to be as follows:
result = {17-Q2: 1(1.00), 17-Q3: 2(2.00), 17-Q4: 5(5.00), 18-Q1: 6(6.00)}
Following code with a single while loop could help:
from collections import OrderedDict
final_dict = dict()
i = 0 # Initializing the counter
while (i<len(list1)):
# Updating the final dict
final_dict.update({list1[i]:str(list2[i+1]) + "(" + str(list1[i+1]) + ")"})
i += 2 # Incrementing by two in order to land on the alternative index
# If u want sorted dictionary
sorted_dict = OrderedDict(sorted(final_dict.items()))
print (sorted_dict)

Resources