Dynamically build key: value lookup for list of variables - python

Suppose I have some variables:
a, b, c, d, e = range(5)
I want to save the values of these variables in a file for later inspection. One way I thought to do this was:
lookup = {
'a': a,
'b': b,
'c': c,
'd': d,
'e': e
}
As you might imagine, with a large number of variables, this could get tedious. And, yes, I know many editors have functionality to make this kind of copy-paste action easy. But I'm looking for the standard, "Pythonic" way of dynamically building a key: value lookup where the key is the variable's name and the value is the variable's, well, value!
I thought about this:
>>> {var.__name__: var for var in [a, b, c, d, e]}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <dictcomp>
AttributeError: 'int' object has no attribute '__name__'
I'm not surprised this didn't work, because integer variables are constants (I'm not sure of the exact way to describe things):
>>> a = 1
>>> b = 1
>>> a is b
True
>>> b is a
True
>>> a == b
True
How might I accomplish this?

import itertools
data = range(5)
result = {f"a{count}": value for count, value in zip(itertools.count(1), data)}
print(result)
Output:
{'a1': 0, 'a2': 1, 'a3': 2, 'a4': 3, 'a5': 4}

You might want to look into locals() and inspect. The result could be i.e.:
>>> from inspect import ismodule
>>> a = 1
>>> b = 1
>>> dict((k, v) for k, v in locals().items() if not k.startswith("__") and not callable(v) and not ismodule(v))
{'a': 1, 'b': 1}
But to get it right you might need to add some additional conditions, and also you will have to watch out for mutable objects or values, as in this case those would mutate and you would not preserve the earlier value for later inspection. Serialization or copying them could help.

You could tackle it from the other direction and save all the local variables, using locals() For example
import json
def foo(a=None, bb=None):
ccc='lots of c'; de=42
print( json.dumps( locals() ))
foo() generates {"a": null, "bb": null, "ccc": "lots of c", "de": 42}
( json.dumps is one way to serialize a dict, and will work only for simple variables that can be converted to JSON)
Another way to just get some variables would be
print( json.dumps( dict( a=a, b=b, c=c) ))

Here is another way using ascii_lowercase from string module:
import string
alphabets = iter(string.ascii_lowercase)
lookup = {next(alphabets): x for x in range(5)}
print(lookup)
# {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4}

Related

Python dict grouping

I am trying to develop a code that groups the dict, the code works alone but I want to make it as a function; and I want to add a code to check the duplicates and just print one of them.
a_dict = {'A': 1, 'B': 2, 'C': 3}
def fun_dict():
b_dict = {}
for i, v in a_dict.items():
b_dict[v] = [i] if v not in b_dict.keys() else b_dict[v] + [i]
b_dict = fun_dict(a_dict)
print(b_dict)
Error:
Traceback (most recent call last):
File "C:\xxx.py", line 11, in <module>
b_dict = fun_dict(a_dict)
TypeError: fun_dict() takes 0 positional arguments but 1 was given
You need to make the function accepting the argument as well as returning b_dict:
a_dict = {'A': 1, 'B': 2, 'C': 3}
def fun_dict(a_dict):
b_dict = {}
for i, v in a_dict.items():
b_dict[v] = [i] if v not in b_dict.keys() else b_dict[v] + [i]
return b_dict
b_dict = fun_dict(a_dict)
print(b_dict)
Out:
{1: ['A'], 2: ['B'], 3: ['C']}
You did not define a parameter for your method, and you are trying to send one. Thats why you are getting an error.
Your a_dict variable is a global variable, you dont have to send it as a parameter.
Your b_dict variable is defined in your fun_dict function, and you can not print it outside of your function

Is it bad practice to iterate through a dict or its keys and change values?

In python 2 I used:
d = {'a': 1, 'b': 2, 'c': 3}
for k in d.keys():
d[k] = d[k] * 2
print(d)
# {'a': 2, 'b': 4, 'c': 6}
In python 3 I use:
d = {'a': 1, 'b': 2, 'c': 3}
for k in d:
d[k] = d[k] * 2
print(d)
# {'a': 2, 'b': 4, 'c': 6}
This new syntax gives me the feeling I'm iterating over something (the dict) and modifying it, which is bad. But in truth I'm only iterating over the keys, so this should not bring any trouble, right?
Iterating over a dictionary and modifying values is perfectly fine. Each time you call dict.__getitem__ / dict.__setitem__, or respectively its syntactic sugar dict[] / dict[] = ..., the value for a key is retrieved. You can overwrite values for keys as you iterate items, as changing values does not change key hashes and therefore does not impact the iterator.
What's not fine is adding or removing keys as you iterate over a view of a dictionary. The reason why this is problematic is given in the docs:
The objects returned by dict.keys(), dict.values() and dict.items()
are view objects. They provide a dynamic view on the dictionary’s
entries, which means that when the dictionary changes, the view
reflects these changes.
I don't see a reason why it should be bad practice if you do not change the set of keys of the dictionary. Your examples do not do that, so they are just fine.
The things starts getting messy if your changing involves adding or removing elements:
d = {}
k = d.keys()
i = iter(k)
d[42] = 23
next(i)
This will raise an exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: dictionary changed size during iteration
And your example can probably be improved a little by iterating over the items instead of the keys alone:
d = dict(a=1, b=2, c=3)
for k, v in d.items():
d[k] = v * 2
or in the special case of your "just doubling":
for k in d.keys():
d[k] *= 2
But I guess your real use case is probably more complex.
EDIT: Be aware that if you are still using Python2, you should use .iteritems() and .iterkeys() instead of .items() and .keys().

Python: How to only pass needed arguments into function from a dictionary?

Assume now I have a well-defined function
def f1(a):
...
and i can not change its definition. Now I want to call it with a argument from a dictionary
D={ 'a':1 , 'b':2}
If I call it like
f1(**D)
there will be a syntax error (because b is not defined in f1). My question is if I can find a smart way to let the function to find the argument it needed only?
You can use inspect.getargspec:
d = {'a': 5, 'b': 6, 'c': 7}
def f(a,b):
return a + b
f(*[d[arg] for arg in inspect.getargspec(f).args])
which gives 11.
Thanks Eugene, getargspec is legacy, instead you should use getfullargspec which has the same usage in this case.
You could get the argument names from the function's code object, then lookup the values for them in the dictionary:
>>> def f(a):
... b = 2
... return a + b
...
>>> D = {'a': 1, 'b': 2, 'c': 3}
>>> nargs = f.__code__.co_argcount
>>> varnames = f.__code__.co_varnames
>>> f(*(D[arg] for arg in varnames[:nargs]))
3
Here is an example of function that got a dictionary and use only the 'b' key :
In [14]: def f1(mydict):
...: return mydict['b']
...:
In [15]: x={'a': 1, 'b':2}
In [16]: f1(x)
Out[16]: 2

Remove key from dictionary in Python returning new dictionary

I have a dictionary
d = {'a':1, 'b':2, 'c':3}
I need to remove a key, say c and return the dictionary without that key in one function call
{'a':1, 'b':2}
d.pop('c') will return the key value - 3 - instead of the dictionary.
I am going to need one function solution if it exists, as this will go into comprehensions
How about this:
{i:d[i] for i in d if i!='c'}
It's called Dictionary Comprehensions and it's available since Python 2.7.
or if you are using Python older than 2.7:
dict((i,d[i]) for i in d if i!='c')
Why not roll your own? This will likely be faster than creating a new one using dictionary comprehensions:
def without(d, key):
new_d = d.copy()
new_d.pop(key)
return new_d
If you need an expression that does this (so you can use it in a lambda or comprehension) then you can use this little hack trick: create a tuple with the dictionary and the popped element, and then get the original item back out of the tuple:
(foo, foo.pop(x))[0]
For example:
ds = [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}]
[(d, d.pop('c'))[0] for d in ds]
assert ds == [{'a': 1, 'b': 2}, {'a': 4, 'b': 5}]
Note that this actually modifies the original dictionary, so despite being a comprehension, it's not purely functional.
When you invoke pop the original dictionary is modified in place.
You can return that one from your function.
>>> a = {'foo': 1, 'bar': 2}
>>> a.pop('foo')
1
>>> a
{'bar': 2}
solution from me
item = dict({"A": 1, "B": 3, "C": 4})
print(item)
{'A': 1, 'B': 3, 'C': 4}
new_dict = (lambda d: d.pop('C') and d)(item)
print(new_dict)
{'A': 1, 'B': 3}
this will work,
(lambda dict_,key_:dict_.pop(key_,True) and dict_)({1:1},1)
EDIT
this will drop the key if exist in the dictionary and will return the dictionary without the key,value pair
in python there are functions that alter an object in place, and returns a value instead of the altered object, {}.pop function is an example.
we can use a lambda function as in the example, or more generic below
(lambda func:obj:(func(obj) and False) or obj)
to alter this behavior, and get a the expected behavior.

Delete an element from a dictionary

How do I delete an item from a dictionary in Python?
Without modifying the original dictionary, how do I obtain another dict with the item removed?
See also How can I remove a key from a Python dictionary? for the specific issue of removing an item (by key) that may not already be present.
The del statement removes an element:
del d[key]
Note that this mutates the existing dictionary, so the contents of the dictionary changes for anybody else who has a reference to the same instance. To return a new dictionary, make a copy of the dictionary:
def removekey(d, key):
r = dict(d)
del r[key]
return r
The dict() constructor makes a shallow copy. To make a deep copy, see the copy module.
Note that making a copy for every dict del/assignment/etc. means you're going from constant time to linear time, and also using linear space. For small dicts, this is not a problem. But if you're planning to make lots of copies of large dicts, you probably want a different data structure, like a HAMT (as described in this answer).
pop mutates the dictionary.
>>> lol = {"hello": "gdbye"}
>>> lol.pop("hello")
'gdbye'
>>> lol
{}
If you want to keep the original you could just copy it.
I think your solution is best way to do it. But if you want another solution, you can create a new dictionary with using the keys from old dictionary without including your specified key, like this:
>>> a
{0: 'zero', 1: 'one', 2: 'two', 3: 'three'}
>>> {i:a[i] for i in a if i!=0}
{1: 'one', 2: 'two', 3: 'three'}
There're a lot of nice answers, but I want to emphasize one thing.
You can use both dict.pop() method and a more generic del statement to remove items from a dictionary. They both mutate the original dictionary, so you need to make a copy (see details below).
And both of them will raise a KeyError if the key you're providing to them is not present in the dictionary:
key_to_remove = "c"
d = {"a": 1, "b": 2}
del d[key_to_remove] # Raises `KeyError: 'c'`
and
key_to_remove = "c"
d = {"a": 1, "b": 2}
d.pop(key_to_remove) # Raises `KeyError: 'c'`
You have to take care of this:
by capturing the exception:
key_to_remove = "c"
d = {"a": 1, "b": 2}
try:
del d[key_to_remove]
except KeyError as ex:
print("No such key: '%s'" % ex.message)
and
key_to_remove = "c"
d = {"a": 1, "b": 2}
try:
d.pop(key_to_remove)
except KeyError as ex:
print("No such key: '%s'" % ex.message)
by performing a check:
key_to_remove = "c"
d = {"a": 1, "b": 2}
if key_to_remove in d:
del d[key_to_remove]
and
key_to_remove = "c"
d = {"a": 1, "b": 2}
if key_to_remove in d:
d.pop(key_to_remove)
but with pop() there's also a much more concise way - provide the default return value:
key_to_remove = "c"
d = {"a": 1, "b": 2}
d.pop(key_to_remove, None) # No `KeyError` here
Unless you use pop() to get the value of a key being removed you may provide anything, not necessary None.
Though it might be that using del with in check is slightly faster due to pop() being a function with its own complications causing overhead. Usually it's not the case, so pop() with default value is good enough.
As for the main question, you'll have to make a copy of your dictionary, to save the original dictionary and have a new one without the key being removed.
Some other people here suggest making a full (deep) copy with copy.deepcopy(), which might be an overkill, a "normal" (shallow) copy, using copy.copy() or dict.copy(), might be enough. The dictionary keeps a reference to the object as a value for a key. So when you remove a key from a dictionary this reference is removed, not the object being referenced. The object itself may be removed later automatically by the garbage collector, if there're no other references for it in the memory. Making a deep copy requires more calculations compared to shallow copy, so it decreases code performance by making the copy, wasting memory and providing more work to the GC, sometimes shallow copy is enough.
However, if you have mutable objects as dictionary values and plan to modify them later in the returned dictionary without the key, you have to make a deep copy.
With shallow copy:
def get_dict_wo_key(dictionary, key):
"""Returns a **shallow** copy of the dictionary without a key."""
_dict = dictionary.copy()
_dict.pop(key, None)
return _dict
d = {"a": [1, 2, 3], "b": 2, "c": 3}
key_to_remove = "c"
new_d = get_dict_wo_key(d, key_to_remove)
print(d) # {"a": [1, 2, 3], "b": 2, "c": 3}
print(new_d) # {"a": [1, 2, 3], "b": 2}
new_d["a"].append(100)
print(d) # {"a": [1, 2, 3, 100], "b": 2, "c": 3}
print(new_d) # {"a": [1, 2, 3, 100], "b": 2}
new_d["b"] = 2222
print(d) # {"a": [1, 2, 3, 100], "b": 2, "c": 3}
print(new_d) # {"a": [1, 2, 3, 100], "b": 2222}
With deep copy:
from copy import deepcopy
def get_dict_wo_key(dictionary, key):
"""Returns a **deep** copy of the dictionary without a key."""
_dict = deepcopy(dictionary)
_dict.pop(key, None)
return _dict
d = {"a": [1, 2, 3], "b": 2, "c": 3}
key_to_remove = "c"
new_d = get_dict_wo_key(d, key_to_remove)
print(d) # {"a": [1, 2, 3], "b": 2, "c": 3}
print(new_d) # {"a": [1, 2, 3], "b": 2}
new_d["a"].append(100)
print(d) # {"a": [1, 2, 3], "b": 2, "c": 3}
print(new_d) # {"a": [1, 2, 3, 100], "b": 2}
new_d["b"] = 2222
print(d) # {"a": [1, 2, 3], "b": 2, "c": 3}
print(new_d) # {"a": [1, 2, 3, 100], "b": 2222}
The del statement is what you're looking for. If you have a dictionary named foo with a key called 'bar', you can delete 'bar' from foo like this:
del foo['bar']
Note that this permanently modifies the dictionary being operated on. If you want to keep the original dictionary, you'll have to create a copy beforehand:
>>> foo = {'bar': 'baz'}
>>> fu = dict(foo)
>>> del foo['bar']
>>> print foo
{}
>>> print fu
{'bar': 'baz'}
The dict call makes a shallow copy. If you want a deep copy, use copy.deepcopy.
Here's a method you can copy & paste, for your convenience:
def minus_key(key, dictionary):
shallow_copy = dict(dictionary)
del shallow_copy[key]
return shallow_copy
… how can I delete an item from a dictionary to return a copy (i.e., not modifying the original)?
A dict is the wrong data structure to use for this.
Sure, copying the dict and popping from the copy works, and so does building a new dict with a comprehension, but all that copying takes time—you've replaced a constant-time operation with a linear-time one. And all those copies alive at once take space—linear space per copy.
Other data structures, like hash array mapped tries, are designed for exactly this kind of use case: adding or removing an element returns a copy in logarithmic time, sharing most of its storage with the original.1
Of course there are some downsides. Performance is logarithmic rather than constant (although with a large base, usually 32-128). And, while you can make the non-mutating API identical to dict, the "mutating" API is obviously different. And, most of all, there's no HAMT batteries included with Python.2
The pyrsistent library is a pretty solid implementation of HAMT-based dict-replacements (and various other types) for Python. It even has a nifty evolver API for porting existing mutating code to persistent code as smoothly as possible. But if you want to be explicit about returning copies rather than mutating, you just use it like this:
>>> from pyrsistent import m
>>> d1 = m(a=1, b=2)
>>> d2 = d1.set('c', 3)
>>> d3 = d1.remove('a')
>>> d1
pmap({'a': 1, 'b': 2})
>>> d2
pmap({'c': 3, 'a': 1, 'b': 2})
>>> d3
pmap({'b': 2})
That d3 = d1.remove('a') is exactly what the question is asking for.
If you've got mutable data structures like dict and list embedded in the pmap, you'll still have aliasing issues—you can only fix that by going immutable all the way down, embedding pmaps and pvectors.
1. HAMTs have also become popular in languages like Scala, Clojure, Haskell because they play very nicely with lock-free programming and software transactional memory, but neither of those is very relevant in Python.
2. In fact, there is an HAMT in the stdlib, used in the implementation of contextvars. The earlier withdrawn PEP explains why. But this is a hidden implementation detail of the library, not a public collection type.
d = {1: 2, '2': 3, 5: 7}
del d[5]
print 'd = ', d
Result: d = {1: 2, '2': 3}
Using del you can remove a dict value passing the key of that value
Link:
del method
del dictionary['key_to_del']
Simply call del d['key'].
However, in production, it is always a good practice to check if 'key' exists in d.
if 'key' in d:
del d['key']
No, there is no other way than
def dictMinus(dct, val):
copy = dct.copy()
del copy[val]
return copy
However, often creating copies of only slightly altered dictionaries is probably not a good idea because it will result in comparatively large memory demands. It is usually better to log the old dictionary(if even necessary) and then modify it.
# mutate/remove with a default
ret_val = body.pop('key', 5)
# no mutation with a default
ret_val = body.get('key', 5)
Here a top level design approach:
def eraseElement(d,k):
if isinstance(d, dict):
if k in d:
d.pop(k)
print(d)
else:
print("Cannot find matching key")
else:
print("Not able to delete")
exp = {'A':34, 'B':55, 'C':87}
eraseElement(exp, 'C')
I'm passing the dictionary and the key I want into my function, validates if it's a dictionary and if the key is okay, and if both exist, removes the value from the dictionary and prints out the left-overs.
Output: {'B': 55, 'A': 34}
Hope that helps!
>>> def delete_key(dict, key):
... del dict[key]
... return dict
...
>>> test_dict = {'one': 1, 'two' : 2}
>>> print delete_key(test_dict, 'two')
{'one': 1}
>>>
this doesn't do any error handling, it assumes the key is in the dict, you might want to check that first and raise if its not
Below code snippet will help you definitely, I have added comments in each line which will help you in understanding the code.
def execute():
dic = {'a':1,'b':2}
dic2 = remove_key_from_dict(dic, 'b')
print(dict2) # {'a': 1}
print(dict) # {'a':1,'b':2}
def remove_key_from_dict(dictionary_to_use, key_to_delete):
copy_of_dict = dict(dictionary_to_use) # creating clone/copy of the dictionary
if key_to_delete in copy_of_dict : # checking given key is present in the dictionary
del copy_of_dict [key_to_delete] # deleting the key from the dictionary
return copy_of_dict # returning the final dictionary
or you can also use dict.pop()
d = {"a": 1, "b": 2}
res = d.pop("c") # No `KeyError` here
print (res) # this line will not execute
or the better approach is
res = d.pop("c", "key not found")
print (res) # key not found
print (d) # {"a": 1, "b": 2}
res = d.pop("b", "key not found")
print (res) # 2
print (d) # {"a": 1}
Solution 1: with deleting
info = {'country': 'Iran'}
country = info.pop('country') if 'country' in info else None
Solution 2: without deleting
info = {'country': 'Iran'}
country = info.get('country') or None
Here's another variation using list comprehension:
original_d = {'a': None, 'b': 'Some'}
d = dict((k,v) for k, v in original_d.iteritems() if v)
# result should be {'b': 'Some'}
The approach is based on an answer from this post:
Efficient way to remove keys with empty strings from a dict
For Python 3 this is
original_d = {'a': None, 'b': 'Some'}
d = dict((k,v) for k, v in original_d.items() if v)
print(d)
species = {'HI': {'1': (1215.671, 0.41600000000000004),
'10': (919.351, 0.0012),
'1025': (1025.722, 0.0791),
'11': (918.129, 0.0009199999999999999),
'12': (917.181, 0.000723),
'1215': (1215.671, 0.41600000000000004),
'13': (916.429, 0.0005769999999999999),
'14': (915.824, 0.000468),
'15': (915.329, 0.00038500000000000003),
'CII': {'1036': (1036.3367, 0.11900000000000001), '1334': (1334.532, 0.129)}}
The following code will make a copy of dict species and delete items which are not in trans_HI
trans_HI=['1025','1215']
for transition in species['HI'].copy().keys():
if transition not in trans_HI:
species['HI'].pop(transition)
In Python 3, 'dict' object has no attribute 'remove'.
But with immutables package, can perform mutations that allow to apply changes to the Map object and create new (derived) Maps:
import immutables
map = immutables.Map(a=1, b=2)
map1 = map.delete('b')
print(map, map1)
# will print:
# <immutables.Map({'b': 2, 'a': 1})>
# <immutables.Map({'a': 1})>
can try my method. In one line.
yourList = [{'key':'key1','version':'1'},{'key':'key2','version':'2'},{'key':'key3','version':'3'}]
resultList = [{'key':dic['key']} for dic in yourList if 'key' in dic]
print(resultList)

Categories