To continue, explain what happens with this code (how you would name this action and is there other way to get same result), why, and what is actually the value of a at end.

a = ['a', 'b','e']
a[2:2] = ['c', 'd']

Dani AI

Generated

Brief answer: assigning to an empty slice is slice assignment (assign-to-slice). It asks the list to replace the slice with the items on the right-hand side, and because the slice length can differ from the replacement sequence the list’s length can change — i.e., this is an in-place insertion of the sequence at that index. The final value becomes ['a', 'b', 'c', 'd', 'e']. (docs.python.org)

Two common ways to get the same result without using assign-to-slice:

# build a new list (creates a new object)
L = ['x', 'y', 'z']
new = L[:1] + ['u', 'v'] + L[1:]
# new == ['x', 'u', 'v', 'y', 'z']
# insert elements one at a time while preserving order
L = ['x', 'y', 'z']
for v in reversed(['u', 'v']):
    L.insert(1, v)
# L == ['x', 'u', 'v', 'y', 'z']

Notes on semantics and performance: slice assignment mutates the existing list; concatenation creates a new list and copies elements. Both inserting (including slice assignment) and slicing operations involve moving elements and are linear in the number of affected items, so they are O(n) in the usual sense; for queue-like workloads use collections.deque instead. (docs.python.org)

Practical tip drawn from the discussion: is right that list.insert only handles a single element at a time; using reversed inserts preserves order. is also right to highlight slicing/concatenation alternatives — for bulk inserts slice assignment is concise and often faster than many repeated insert() calls (benchmarks and CPython discussion show slice-assignment can outperform insert in many cases). For frequent arbitrary-position inserts at scale, pick a different structure or algorithm rather than relying on repeated list inserts. (bugs.python.org)

Recommended Answers

All 3 Replies

a is initialized as

a b e
0 1 2

An easy way to visualize a[2:2] is to consider a[2:3], which references ['e']. Thus, a[2:2] refences the empty list before 'e'. Setting a[2:2] = ['c', 'd'] inserts c&d after 'b', giving the final result of: ['a', 'b', 'c', 'd', 'e'].

This is just called inserting into a list. There are plently of other ways to do this. Python supplies a list.insert() method. You could also pop the end off the list until you reach your desired entry point, extend/append your own list/elements on, and push the back back on.

Sorry, I should have explaind that list.insert() only inserts one element at a time, so you'll need to read in your elements individually for it to work.

Yes, the point is that it is not insert, but append inside. So instead the alternative is slicing and addition. This thread is BTW not a question but food for thought, you might next time wait little time until posting visibly answer, even everyone can see what the result actually is. But I think the real spectrum of slicing is not usually covered by the programming courses. Say for example my old stackoverflow post on most optimized prime sieve: http://stackoverflow.com/questions/3285443/improving-pure-python-prime-sieve-by-recurrence-formula

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.