Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • If I wanted to remove the 1st x elements of a list, what will be better: l = l[6:] or l[:] = l[6:]? Commented Jul 24, 2022 at 18:00
  • The first way works for a list or a string; the second way only works for a list, because slice assignment isn't allowed for strings. Other than that I think the only difference is speed: it looks like it's a little faster the first way. Try it yourself with timeit.timeit() or preferably timeit.repeat(). They are super easy to use and very educational, it's worth getting used to playing with them all the time! Commented Jul 25, 2022 at 19:46
  • Curious about what's the time complexity of doing r[1:1]=['blah'] ? thanks! Commented Sep 1, 2022 at 17:21
  • p[2:3] = 't' works fine ! there should be no TypeError ! Commented Apr 6, 2023 at 11:08
  • Should be read in conjunction with this great answer to stackoverflow.com/questions/31593201/…. Commented Jan 9, 2024 at 12:45

lang-py