Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8726395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:14:07+00:00 2026-06-13T08:14:07+00:00

I work on a class with and embedded list. class a: def __init__(self, n):

  • 0

I work on a class with and embedded list.

class a:
    def __init__(self, n):
        self.l = [1] * n
    def __getitem__(self, i):
        return self.l[i]
    def __delitem__(self, i):
        print type(i)
        print i

I want to use the del operator with the full syntax of slices:

p = a(10)
del p[1:5:2]

The __delitem__ receives a slice object if the parameter is not a single index. How can I use the slice object to iterate through the specified elements?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:14:08+00:00Added an answer on June 13, 2026 at 8:14 am

    The indices method of the slice object will, given the length of the sequence, provide the canonical interpretation of the slice that you can feed to xrange:

    def __delitem__(self, item):
        if isinstance(item, slice):
            for i in xrange(*item.indices(len(self.l))):
                print i
        else:
            print operator.index(item)
    

    The use of slice.indices makes sure that you get correct behavior in cases pointed out by Dunes. Also note that you can pass the slice object to list.__delitem__, so if you only need to do some preprocessing and delegate actual deletion to the underlying list, a “naive” del self.l[i] will in fact work correctly.

    operator.index will make sure that you get an early exception if your __delitem__ receives a non-slice object that cannot be converted to an index.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why doesn't the following work: class CTest(tuple): def __init__(self,arg): if type(arg) is tuple: super(CTest,self).__init__((2,2))
I am using AsyncTask and want to use getApplication() to work with class Application.
Consider the code: class Work { public void DoStuff(string s) { Console.WriteLine(s); // ..
Considering that simple java code which would not work: public class Bar extends AbstractBar{
A college at work created a class with only static methods. Because he wants
Im trying to understand how class generics work and this bit just doesnt make
DataAnnotations does not work with buddy class. The following code always validate true. Why
What is the preferred way to work with Singleton class in multithreaded environment? Suppose
Example for threading queue book Accelerated C# 2008 (CrudeThreadPool class) not work correctly. If
My question is about controller methods (possibly included from an outside class) that work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.