Python ElementTree: How to add SubElement at VERY specific position?

I want to add a subelement to an xml file, but in a very specific position, not appended to the end.

The standard way is:

subi = ET.SubElement(root[0][0], 'subi')

which is fine.

but: Let's say, root[0][0] already has two children, hence accessible via root[0][0][0] and root[0][0][1].

And I want "subi" to become the new middle child, root[0][0][1], making the original second child become the third child root[0][0][2].

Is there a way to do that? (My experiences with life and nature would say no, but I have high hopes for python=)


You can use Element.insert method. It allows you to specify an index.

For example, to insert before the 3rd (index: 2) element:

>>> import xml.etree.ElementTree as ET >>> >>> root = ET.fromstring(''' ... <root> ... <first></first> ... <second></second> ... <third></third> ... </root> ... ''') >>> >>> new = ET.Element('new') >>> root.insert(2, new) # <----------- >>> print(ET.tostring(root)) <root> <first /> <second /> <new /><third /> </root>


  • Parse XML to Table in Python
  • Get XML node based on heritage of element values
  • Generate xml documents using lxml and vary element text and attributes based on logic
  • C# and XPath - how to query
  • Centering a specific element among others with flexbox [duplicate]
  • Is there a way to pivot a customer ID and a their most recent order dates?
  • PHP Handling Namespace with SimpleXML
  • Browserify and Reactify source maps include full local path names
  • Angular2 emit event up to the DOM tree
  • Firebase, only get new children
  • Does “internal” visibility modifier in Kotlin work yet?
  • Copying rows in a database when rows have children
  • Passing information to server-side function in a Google Docs Add On
  • Access the state of control in Winforms from another application
  • Mercurial: Identify file name after rename
  • Why doesnt this Java loop in a thread work?
  • uml Composition relationships to RDF and OWL
  • Who propagate bugfixes across branches (corporate development)?
  • Azure table store snapshot/backup capability
  • xcode don't localize specific strings
  • How to disable all widgets inside Panel or inside Composite?
  • Position: fixed nav does not stay fixed
  • JBoss External Properties Files in Classpath
  • xtable package: Skipping some rows in the output
  • Debug.DrawLine not showing in the GameView
  • htaccess add www if not subdomain, if subdomain remove www
  • Play WS (2.2.1): post/put large request
  • ilmerge with a PFX file
  • Xamarin Forms - UWP Fonts
  • Change JButton Shape while respecting Look And Feel
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Deserializing XML into class C#
  • How can I estimate amount of memory left with calling System.gc()?
  • Is there a mandatory requirement to switch app.yaml?
  • Hits per day in Google Big Query
  • Running Map reduces the dimensions of the matrices
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML