Re: ok, I feel stupid, but there must be a better way than this! (finding name of unique key in dict)

2023-01-21 Thread Dino
I learned new things today and I thank you all for your responses. Please consider yourself thanked individually. Dino On 1/20/2023 10:29 AM, Dino wrote: let's say I have this list of nested dicts: -- https://mail.python.org/mailman/listinfo/python-list

Re: ok, I feel stupid, but there must be a better way than this! (finding name of unique key in dict)

2023-01-20 Thread Rob Cliffe via Python-list
in origListOfDescriptors] I believe that from Python 3.9 onwards this can be written more concisely as: listOfDescriptors = [     { (L := list(D.items())[0])[1] } | {'value' : L[0] }     for D in origListOfDescriptors]     # untested Best wishes Rob Cliffe I actually did i

Re: ok, I feel stupid, but there must be a better way than this! (finding name of unique key in dict)

2023-01-20 Thread Oscar Benjamin
On Fri, 20 Jan 2023 at 17:30, Dino wrote: > > let's say I have this list of nested dicts: > > [ >{ "some_key": {'a':1, 'b':2}}, >{ "some_other_key": {'a':3, 'b':4}} > ] > > I need to turn this into: > > [ >{ "value": "some_key", 'a':1, 'b':2}, >{ "value": "some_other_key", 'a':3, '

Re: ok, I feel stupid, but there must be a better way than this! (finding name of unique key in dict)

2023-01-20 Thread Jon Ribbens via Python-list
On 2023-01-20, Dino wrote: > > let's say I have this list of nested dicts: > > [ >{ "some_key": {'a':1, 'b':2}}, >{ "some_other_key": {'a':3, 'b':4}} > ] > > I need to turn this into: > > [ >{ "value": "some_key", 'a':1, 'b':2}, >{ "value": "some_other_key", 'a':3, 'b':4} > ] [{"v

Re: ok, I feel stupid, but there must be a better way than this! (finding name of unique key in dict)

2023-01-20 Thread Dino
hopefully you get the spirit. "value": cn, "a": cd[cn]["a"], "b": cd[cn]["b"] Anyway, the key point (ooops, a pun) is if there's a more elegant way to do this (i.e. get a reference to the unique key in a dict() when

Re: ok, I feel stupid, but there must be a better way than this! (finding name of unique key in dict)

2023-01-20 Thread Tobiah
On 1/20/23 07:29, Dino wrote: let's say I have this list of nested dicts: [   { "some_key": {'a':1, 'b':2}},   { "some_other_key": {'a':3, 'b':4}} ] I need to turn this into: [   { "value": "some_key", 'a':1, 'b':2},   { "value": "some_other_key", 'a':3, 'b':4} ] This doesn't look like

ok, I feel stupid, but there must be a better way than this! (finding name of unique key in dict)

2023-01-20 Thread Dino
, { "value": "some_other_key", 'a':3, 'b':4} ] I actually did it with: listOfDescriptors = list() for cd in origListOfDescriptors: cn = list(cd.keys())[0] # There must be a better way than this! listOfDescriptors.append({ "value":

Re: There must be a better way (correction)

2013-04-23 Thread Tim Chase
On 2013-04-23 09:30, Tim Chase wrote: > > But a csv.DictReader might still be more efficient. I never > > tested. This is the only place I've used this "optimization". > > It's fast enough. ;) > > I believe the csv module does all the work at c-level, rather than > as pure Python, so it should be

Re: There must be a better way

2013-04-23 Thread Skip Montanaro
> But a csv.DictReader might still be more efficient. Depends on what efficiency you care about. The DictReader class is implemented in Python, and builds a dict for every row. It will never be more efficient CPU-wise than instantiating the csv.reader type directly and only doing what you need.

Re: There must be a better way

2013-04-23 Thread Tim Chase
On 2013-04-23 13:36, Neil Cerutti wrote: > On 2013-04-22, Colin J. Williams wrote: > > Since I'm only interested in one or two columns, the simpler > > approach is probably better. > > Here's a sketch of how one of my projects handles that situation. > I think the index variables are invaluable d

Re: There must be a better way

2013-04-23 Thread Oscar Benjamin
On 23 April 2013 14:36, Neil Cerutti wrote: > On 2013-04-22, Colin J. Williams wrote: >> Since I'm only interested in one or two columns, the simpler >> approach is probably better. > > Here's a sketch of how one of my projects handles that situation. > I think the index variables are invaluable

Re: There must be a better way

2013-04-23 Thread Neil Cerutti
On 2013-04-22, Colin J. Williams wrote: > Since I'm only interested in one or two columns, the simpler > approach is probably better. Here's a sketch of how one of my projects handles that situation. I think the index variables are invaluable documentation, and make it a bit more robust. (Python

Re: There must be a better way

2013-04-22 Thread Colin J. Williams
On 22/04/2013 10:42 AM, Neil Cerutti wrote: On 2013-04-21, Colin J. Williams wrote: On 20/04/2013 9:07 PM, Terry Jan Reedy wrote: On 4/20/2013 8:34 PM, Tim Chase wrote: In 2.x, the csv.reader() class (and csv.DictReader() class) offered a .next() method that is absent in 3.x In Py 3, .next

Re: There must be a better way

2013-04-22 Thread Neil Cerutti
On 2013-04-21, Colin J. Williams wrote: > On 20/04/2013 9:07 PM, Terry Jan Reedy wrote: >> On 4/20/2013 8:34 PM, Tim Chase wrote: >>> In 2.x, the csv.reader() class (and csv.DictReader() class) offered >>> a .next() method that is absent in 3.x >> >> In Py 3, .next was renamed to .__next__ for *al

Re: There must be a better way

2013-04-22 Thread Oscar Benjamin
On 21 April 2013 14:15, Colin J. Williams wrote: > In the end, I used: > > inData= csv.reader(inFile) > > def main(): > > if ver == '2': > headerLine= inData.next() > else: > headerLine= inData.__next__() > ... > for item in inData: > assert len(dataStore) =

Re: There must be a better way

2013-04-21 Thread Colin J. Williams
On 21/04/2013 9:43 AM, Peter Otten wrote: Colin J. Williams wrote: I was seeking some code that would be acceptable to both Python 2.7 and 3.3. In the end, I used: inData= csv.reader(inFile) def main(): if ver == '2': headerLine= inData.next() else: headerLine

Re: There must be a better way

2013-04-21 Thread Colin J. Williams
On 21/04/2013 9:39 AM, Jussi Piitulainen wrote: Colin J. Williams writes: ... It is not usual to have a name with preceding and following udserscores,imn user code. Presumably, there is a rationale for the change from csv.reader.next to csv.reader.__next__. ... I think the user code is suppos

Re: There must be a better way

2013-04-21 Thread Peter Otten
Colin J. Williams wrote: > I was seeking some code that would be acceptable to both Python 2.7 and > 3.3. > > In the end, I used: > > inData= csv.reader(inFile) > > def main(): > if ver == '2': > headerLine= inData.next() > else: > headerLine= inData.__next__() >

Re: There must be a better way

2013-04-21 Thread Jussi Piitulainen
Colin J. Williams writes: ... > It is not usual to have a name with preceding and following > udserscores,imn user code. > > Presumably, there is a rationale for the change from csv.reader.next > to csv.reader.__next__. ... I think the user code is supposed to be next(csv.reader). For example, cu

Re: There must be a better way

2013-04-21 Thread Colin J. Williams
On 20/04/2013 9:07 PM, Terry Jan Reedy wrote: On 4/20/2013 8:34 PM, Tim Chase wrote: In 2.x, the csv.reader() class (and csv.DictReader() class) offered a .next() method that is absent in 3.x In Py 3, .next was renamed to .__next__ for *all* iterators. The intention is that one iterate with fo

Re: There must be a better way

2013-04-20 Thread Terry Jan Reedy
On 4/20/2013 8:34 PM, Tim Chase wrote: In 2.x, the csv.reader() class (and csv.DictReader() class) offered a .next() method that is absent in 3.x In Py 3, .next was renamed to .__next__ for *all* iterators. The intention is that one iterate with for item in iterable or use builtin functions i

Re: There must be a better way

2013-04-20 Thread Tim Chase
On 2013-04-21 00:06, Steven D'Aprano wrote: > On Sat, 20 Apr 2013 19:46:07 -0400, Colin J. Williams wrote: > > > Below is part of a script which shows the changes made to permit > > the script to run on either Python 2.7 or Python 3.2. > > > > I was surprised to see that the CSV next method is no

Re: There must be a better way

2013-04-20 Thread Steven D'Aprano
On Sat, 20 Apr 2013 19:46:07 -0400, Colin J. Williams wrote: > Below is part of a script which shows the changes made to permit the > script to run on either Python 2.7 or Python 3.2. > > I was surprised to see that the CSV next method is no longer available. This makes no sense. What's "the CSV

Re: There must be a better way

2013-04-20 Thread Chris Rebert
On Sat, Apr 20, 2013 at 4:46 PM, Colin J. Williams wrote: > Below is part of a script which shows the changes made to permit the script > to run on either Python 2.7 or Python 3.2. > > I was surprised to see that the CSV next method is no longer available. > > Suggestions welcome. > if ver ==

There must be a better way

2013-04-20 Thread Colin J. Williams
Below is part of a script which shows the changes made to permit the script to run on either Python 2.7 or Python 3.2. I was surprised to see that the CSV next method is no longer available. Suggestions welcome. Colin W. def main(): global inData, inFile if ver == '2': headerL