moving all attributes from one element to another

I want to move all attributes of an element to another element. If the attributes of elements x are x.attrib and attributes = x.attrib, is there a single command that says "take the attribute dictionary of x and attach it to y"? Or do I have to use the .set method for each individual attribute? MM

I want to move all attributes of an element to another element. If the attributes of elements x are x.attrib and attributes = x.attrib, is there a single command that says "take the attribute dictionary of x and attach it to y"?
you still copy the attribute values, but all at once:
y.attrib.update(x.items()) x.attrib.clear()
jens

Am .01.2015, 17:38 Uhr, schrieb jens quade <jq@qdevelop.de>:
you still copy the attribute values, but all at once:
y.attrib.update(x.items()) x.attrib.clear()
Nice trick! y.attrib.update(x.attrib) is also possible. I don't know if there's any performance improvement on not calling the items method. Charlie -- Charlie Clark Managing Director Clark Consulting & Research German Office Kronenstr. 27a Düsseldorf D- 40217 Tel: +49-211-600-3657 Mobile: +49-178-782-6226

Thanks to both Charlie and Jens. This is a really terrific list: lots of prompt, terse, and good advice, and the noise level is remarkably low. A good example for the world at large Martin Mueller Professor emeritus of English and Classics Northwestern University On 1/27/15 10:51 AM, "Charlie Clark" <charlie.clark@clark-consulting.eu> wrote:
Am .01.2015, 17:38 Uhr, schrieb jens quade <jq@qdevelop.de>:
you still copy the attribute values, but all at once:
y.attrib.update(x.items()) x.attrib.clear()
Nice trick!
y.attrib.update(x.attrib) is also possible. I don't know if there's any performance improvement on not calling the items method.
Charlie -- Charlie Clark Managing Director Clark Consulting & Research German Office Kronenstr. 27a Düsseldorf D- 40217 Tel: +49-211-600-3657 Mobile: +49-178-782-6226 _________________________________________________________________ Mailing list for the lxml Python XML toolkit - http://lxml.de/ lxml@lxml.de https://mailman-mail5.webfaction.com/listinfo/lxml

Charlie Clark schrieb am 27.01.2015 um 17:51:
Am .01.2015, 17:38 Uhr, schrieb jens quade:
you still copy the attribute values, but all at once:
y.attrib.update(x.items()) x.attrib.clear()
Nice trick!
y.attrib.update(x.attrib) is also possible. I don't know if there's any performance improvement on not calling the items method.
Not currently - the implementation simply calls .items() internally and iterates over it. https://github.com/lxml/lxml/blob/44ec7b535d1a342b3c4b6070ebd2a4c3e29595f7/s... But I personally find it a bit clearer to say "x.attrib.update(y.attrib)", and if it ever gets further optimised (it's fast enough, so don't hold your breath), it would quite likely happen for this idiom. Stefan

Martin Mueller schrieb am 27.01.2015 um 16:48:
I want to move all attributes of an element to another element. If the attributes of elements x are x.attrib and attributes = x.attrib, is there a single command that says "take the attribute dictionary of x and attach it to y"?
Or do I have to use the .set method for each individual attribute?
element.attrib is a dict-like object, so you can use a.attrib.update(b.attrib). If you can't be sure that a.attrib is empty before you do that and don't want any left-over content, you can .clear() it first. Stefan
participants (4)
-
Charlie Clark
-
jens quade
-
Martin Mueller
-
Stefan Behnel