...`vars` typically returns a `dict`.
So as I understand it, I need to convert the InstanceState-objects to,
say, dicts, in order to print them. However I also want to remove one
of the keys from the output and assumed I could just pop it off each
event dict, thus:
event_dicts = [vars(e) for e in events]
print(type(event_dicts[0]))
event_dicts = [e.pop('_sa_instance_state', None) for e in event_dicts]
print(type(event_dicts[0]))
However, this prints
<class 'dict'>
<class 'sqlalchemy.orm.state.InstanceState'>This is what you have popped.
If I comment out the third line, which pops the unwanted key, I getThen you do not change `event_dicts`.
Hi,
I am using SQLAlchemy to extract some rows from a table of 'events'.
From the call to the DB I get a list of objects of the type
sqlalchemy.orm.state.InstanceState
I would like to print these rows to the terminal using the 'tabulate' package, the documentation for which says
The module provides just one function, tabulate, which takes a list of
lists or another tabular data type as the first argument, and outputs
a nicely formatted plain-text table
So as I understand it, I need to convert the InstanceState-objects to,
say, dicts, in order to print them. However I also want to remove one
of the keys from the output and assumed I could just pop it off each
event dict, thus:
event_dicts = [vars(e) for e in events]
print(type(event_dicts[0]))
event_dicts = [e.pop('_sa_instance_state', None) for e in event_dicts]
print(type(event_dicts[0]))
However, this prints
<class 'dict'>
<class 'sqlalchemy.orm.state.InstanceState'>
If I comment out the third line, which pops the unwanted key, I get
<class 'dict'>
<class 'dict'>
Why does popping one of the keys cause the elements of the list to
revert back to their original class?
Loris Bennett wrote:
Hi,
I am using SQLAlchemy to extract some rows from a table of 'events'.
From the call to the DB I get a list of objects of the type
sqlalchemy.orm.state.InstanceState
I would like to print these rows to the terminal using the
'tabulate'
package, the documentation for which says
The module provides just one function, tabulate, which takes a
list of
lists or another tabular data type as the first argument, and outputs
a nicely formatted plain-text table
So as I understand it, I need to convert the InstanceState-objects
to,
say, dicts, in order to print them. However I also want to remove one
of the keys from the output and assumed I could just pop it off each
event dict, thus:
event_dicts = [vars(e) for e in events]
print(type(event_dicts[0]))
event_dicts = [e.pop('_sa_instance_state', None) for e in event_dicts] >> print(type(event_dicts[0]))
vars() returns the __dict__ attribute of the object. It may not be a
good idea to modify that dictionary directly (it will also affect the object), although it might be OK if you're not going to do anything
else with the original objects. To be safer, you could copy the event objects:
event_dicts = [dict(vars(e)) for e in events]
or:
event_dicts = [vars(e).copy()]
However, this prints
<class 'dict'>
<class 'sqlalchemy.orm.state.InstanceState'>
If I comment out the third line, which pops the unwanted key, I get
<class 'dict'>
<class 'dict'>
Why does popping one of the keys cause the elements of the list to
revert back to their original class?
As Dieter pointed out, the main problem here is that pop() returns the
value removed, not the dictionary with the rest of the values. You
probably want something more like:
for e in event_dicts:
del e['_sa_instance_state']
(There's not really any point popping the value if you're not going to
do anything with it - just delete the key from the dictionary)
Yes, I was mistakenly thinking that the popping the element would
leave me with the dict minus the popped key-value pair.
Seem like there is no such function.
{'first': 1, 'third': 3}remaining({"first": 1, "second": 2, "third": 3}, "second")
Loris wrote:
"Yes, I was mistakenly thinking that the popping the element would leave
me with the dict minus the popped key-value pair. Seem like there is no
such function."
Others have tried to explain and pointed out you can del and then use the changed dict.
But consider the odd concept of writing your own trivial function.
def remaining(adict, anitem):
_ = adict.pop(anitem)
# alternatively duse del on dict and item
return adict
{'first': 1, 'third': 3}remaining({"first": 1, "second": 2, "third": 3}, "second")
Or do you want to be able to call it as in dict.remaining(key) by
subclassing your own variant of dict and adding a similar method?
On 2024-03-22, Loris Bennett via Python-list <python-list@python.org> wrote:
Yes, I was mistakenly thinking that the popping the element would
leave me with the dict minus the popped key-value pair.
It does.
Seem like there is no such function.
Yes, there is. You can do that with either pop or del:
>>> d = {'a':1, 'b':2, 'c':3}
>>> d
{'a': 1, 'b': 2, 'c': 3}
>>> d.pop('b')
2
>>> d
{'a': 1, 'c': 3}
>>> d = {'a':1, 'b':2, 'c':3}
>>> del d['b']
>>> d
{'a': 1, 'c': 3}
In both cases, you're left with the dict minus the key/value pair.
In the first case, the deleted value printed by the REPL because it
was returned by the expression "d.pop('b')" (a method call).
In the second case is no value shown by the REPL because "del d['b']"
is a statement not an expression.
Grant Edwards <grant.b.edwards@gmail.com> writes:I apologize if this has already been mentioned in this thread, but are
On 2024-03-22, Loris Bennett via Python-list <python-list@python.org> wrote: >>
Yes, I was mistakenly thinking that the popping the element would
leave me with the dict minus the popped key-value pair.
It does.
Indeed, but I was thinking in the context of
dict_list = [d.pop('a') for d in dict_list]
and incorrectly expecting to get a list of 'd' without key 'a', instead
of a list of the 'd['a]'.
On 25/03/2024 01.56, Loris Bennett wrote:
Grant Edwards <grant.b.edwards@gmail.com> writes:I apologize if this has already been mentioned in this thread, but are
On 2024-03-22, Loris Bennett via Python-list <python-list@python.org> wrote:Indeed, but I was thinking in the context of
Yes, I was mistakenly thinking that the popping the element would
leave me with the dict minus the popped key-value pair.
It does.
dict_list = [d.pop('a') for d in dict_list]
and incorrectly expecting to get a list of 'd' without key 'a',
instead
of a list of the 'd['a]'.
you aware of "d.keys()" and "d.values"?
['fa', 'mi', 'do', 're']d = {}
d['do'] = 'a deer, a female deer'
d['re'] = 'a drop of golden sunshine'
d['mi'] = 'a name I call myself'
d['fa'] = 'a long, long way to run'
d.keys()
['a long, long way to run', 'a name I call myself', 'a deer, a female deer', 'a drop of golden sunshine']d.values()
"Michael F. Stemper" <michael.stemper@gmail.com> writes:
On 25/03/2024 01.56, Loris Bennett wrote:
Grant Edwards <grant.b.edwards@gmail.com> writes:I apologize if this has already been mentioned in this thread, but are
On 2024-03-22, Loris Bennett via Python-list <python-list@python.org> wrote:Indeed, but I was thinking in the context of
Yes, I was mistakenly thinking that the popping the element would
leave me with the dict minus the popped key-value pair.
It does.
dict_list = [d.pop('a') for d in dict_list]
and incorrectly expecting to get a list of 'd' without key 'a',
instead
of a list of the 'd['a]'.
you aware of "d.keys()" and "d.values"?
['fa', 'mi', 'do', 're']d = {}
d['do'] = 'a deer, a female deer'
d['re'] = 'a drop of golden sunshine'
d['mi'] = 'a name I call myself'
d['fa'] = 'a long, long way to run'
d.keys()
['a long, long way to run', 'a name I call myself', 'a deer, a female deer', 'a drop of golden sunshine']d.values()
Yes, I am, thank you. However, I didn't want either the keys or the
values. Instead I wanted to remove a key within a list comprehension.
Grant Edwards <grant.b.edwards@gmail.com> writes:
On 2024-03-22, Loris Bennett via Python-list <python-list@python.org> wrote: >>
Yes, I was mistakenly thinking that the popping the element would
leave me with the dict minus the popped key-value pair.
It does.
Indeed, but I was thinking in the context of
dict_list = [d.pop('a') for d in dict_list]
and incorrectly expecting to get a list of 'd' without key 'a', instead
of a list of the 'd['a]'.
Thanks for pointing out 'del'. My main problem, however, was
failing to realise that the list comprehension is populated by the
return value of the 'pop', not the popped dict.
Loris wrote:
"Yes, I was mistakenly thinking that the popping the element would leave
me with the dict minus the popped key-value pair. Seem like there is no
such function."
Others have tried to explain and pointed out you can del and then use the changed dict.
But consider the odd concept of writing your own trivial function.
def remaining(adict, anitem):
_ = adict.pop(anitem)
# alternatively duse del on dict and item
return adict
{'first': 1, 'third': 3}remaining({"first": 1, "second": 2, "third": 3}, "second")
Or do you want to be able to call it as in dict.remaining(key) byNo, 'del' does indeed do what I wanted, although I have now decided I
subclassing your own variant of dict and adding a similar method?
On 2024-03-22, Loris Bennett via Python-list <python-list@python.org>wrote:
Yes, I was mistakenly thinking that the popping the element would
leave me with the dict minus the popped key-value pair.
It does.
Seem like there is no such function.
Yes, there is. You can do that with either pop or del:
>>> d = {'a':1, 'b':2, 'c':3}
>>> d
{'a': 1, 'b': 2, 'c': 3}
>>> d.pop('b')
2
>>> d
{'a': 1, 'c': 3}
>>> d = {'a':1, 'b':2, 'c':3}
>>> del d['b']
>>> d
{'a': 1, 'c': 3}
In both cases, you're left with the dict minus the key/value pair.
In the first case, the deleted value printed by the REPL because it
was returned by the expression "d.pop('b')" (a method call).
In the second case is no value shown by the REPL because "del d['b']"
is a statement not an expression.
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 920 |
Nodes: | 10 (1 / 9) |
Uptime: | 83:53:22 |
Calls: | 12,188 |
Calls today: | 3 |
Files: | 186,526 |
Messages: | 2,237,040 |