I must be missing something simple, but...
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> d = dict()
>>> d['squib'] = "007"
>>> # I forget that 'squib' is my key to retrieve the only element in d
...
>
On 2016-03-18 17:33, Fillmore wrote:
> >>> d = dict()
> >>> d['squib'] = "007"
> >>> key = d.items()[0]
I posted a similar question about 1-element-sets[1] a while back and
Peter Otten & Rene Pijlman both suggested
>>> s = set(["hello"])
>>> element, = s
which, in your case would translate t
OK, so ... I'll bite!
>>> d = {"squib": "007"}
>>> key, = d
Why exactly does this work?
I understand why the following three are similar and why they all
work alike in this situation:
key, = d
(key,) = d
[key] = d
I also, intuitively understand that, if the dictionary d contains
mo
On Saturday 19 March 2016 04:42:29 Chris Angelico wrote:
> On Sat, Mar 19, 2016 at 7:29 PM, Steven D'Aprano
wrote:
> > On Sat, 19 Mar 2016 12:36 pm, Chris Angelico wrote:
> >> So unpacking will give you those keys - in an arbitrary order. Of
> >> course, you don't care about the order when there
I think you're looking for something like popitem().
>>> d = {'asdf':1}
>>> d.popitem()[0]
'asdf'
Cheers
On Fri, Mar 18, 2016 at 2:33 PM, Fillmore
wrote:
>
> I must be missing something simple, but...
>
> Python 3.4.0 (default, Apr 11 2014, 13:05:11)
> [GCC 4.8.2] on linux
> Type "help", "cop
On Sat, Mar 19, 2016 at 7:29 PM, Steven D'Aprano wrote:
> On Sat, 19 Mar 2016 12:36 pm, Chris Angelico wrote:
>
>> So unpacking will give you those keys - in an arbitrary order. Of
>> course, you don't care about the order when there's only one.
>
> But what if you want the key in reverse order?
>
On Sat, 19 Mar 2016 12:36 pm, Chris Angelico wrote:
> So unpacking will give you those keys - in an arbitrary order. Of
> course, you don't care about the order when there's only one.
But what if you want the key in reverse order?
# Standard order
[key] = mydict
# Reverse order
[yɘʞ] = mydict
On Fri, Mar 18, 2016 at 5:33 PM, Fillmore wrote:
>
> I must be missing something simple, but...
>
> Python 3.4.0 (default, Apr 11 2014, 13:05:11)
> [GCC 4.8.2] on linux
> Type "help", "copyright", "credits" or "license" for more information.
d = dict()
d['squib'] = "007"
# I forget
On Sat, Mar 19, 2016 at 8:33 AM, Fillmore wrote:
> what am I missing? I don't want to iterate over the dictionary.
> I know that there's only one element and I need to retrieve the key
You could try:
>>> next(iter(d))
Is that clean enough for usage?
ChrisA
--
https://mail.python.org/mailman/l
On 3/18/2016 5:39 PM, Daniel Wilcox wrote:
I think you're looking for something like popitem().
d = {'asdf':1}
d.popitem()[0]
'asdf'
Only if he wants the item removed.
--
Terry Jan Reedy
--
https://mail.python.org/mailman/listinfo/python-list
OK, this seems to do the trick, but boy is it a lot of code. Anythong more
pythonic?
>>> l = list(d.items())
>>> l
[('squib', '007')]
>>> l[0]
('squib', '007')
>>> l[0][0]
'squib'
>>>
On 03/18/2016 05:33 PM, Fillmore wrote:
I must be missing something simple, but...
Python 3.4.0 (default,
>> But, I still don't understand why this works and can't puzzle it
>> out. I see a sequence on the left of the assignment operator and a
>> dictionary (mapping) on the right.
>
>When you iterate over a dictionary, you get its keys:
>
>scores = {"Fred": 10, "Joe": 5, "Sam": 8}
>for person in scor
On Fri, Mar 18, 2016 at 6:08 PM, Fillmore
wrote:
>
> OK, this seems to do the trick, but boy is it a lot of code. Anythong more
> pythonic?
>
> >>> l = list(d.items())
> >>> l
> [('squib', '007')]
> >>> l[0]
> ('squib', '007')
> >>> l[0][0]
> 'squib'
Maybe this:
l = list(d.items())[0][0]
>
> >
On Sat, Mar 19, 2016 at 12:27 PM, Martin A. Brown wrote:
> But, I still don't understand why this works and can't puzzle it
> out. I see a sequence on the left of the assignment operator and a
> dictionary (mapping) on the right.
When you iterate over a dictionary, you get its keys:
scores = {"
On Sat, Mar 19, 2016 at 10:03 AM, Tim Chase
wrote:
> >>> d = {"squib": "007"}
> >>> key, = d
> >>> key
> 'squib'
>
> I'd put a comment on the line to make it clear what's going on since
> that comma is easy to miss, but based on Alex Martelli's testing[2],
> it was the fastest of the propo
15 matches
Mail list logo