3 Replies - 564 Views - Last Post: 28 March 2014 - 07:39 AM Rate Topic: -----

#1 al.25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-March 14

Removing items from a list with enumerate

Posted 28 March 2014 - 02:39 AM

Thanks for the replies, everyone! It seems to be working now more reliably. I encountered a random bluescreen but it seemed like a once off this time. No bluescreens since.

Anyway, another question relating to coding practices with enumerate().

In going through a list and checking to see if an input matches anything in the list followed by removing it, how would I go about doing so?

Currently, I have:
    i = 0
    for d, t in todolist:
        if t == name:
            todolist.pop(i)
            return True
        i += 1"""


But I could also do this, right?

    
    for i, t in enumerate(todolist):
        if t[1] == name:
            todolist.pop(i)
            return True


Which is better to do?

It's also probably been explained in my lectures and I could probably read a lot about it, but what's the convention on commenting? I've seen my lecturer do something like I've been doing:

""" <Description of function>

function(type, type) -> type
"""


Anything else I should know of?

Is This A Good Question/Topic? 0
  • +

Replies To: Removing items from a list with enumerate

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11224
  • View blog
  • Posts: 19,242
  • Joined: 19-March 11

Re: Removing items from a list with enumerate

Posted 28 March 2014 - 05:12 AM

Note: I've split thie off from your other topic, since it has nothing at all to do with it.
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11224
  • View blog
  • Posts: 19,242
  • Joined: 19-March 11

Re: Removing items from a list with enumerate

Posted 28 March 2014 - 05:29 AM

View Postal.25, on 28 March 2014 - 04:39 AM, said:

In going through a list and checking to see if an input matches anything in the list followed by removing it, how would I go about doing so?

Currently, I have:
    i = 0
    for d, t in todolist:
        if t == name:
            todolist.pop(i)
            return True
        i += 1"""


But I could also do this, right?

    
    for i, t in enumerate(todolist):
        if t[1] == name:
            todolist.pop(i)
            return True


Which is better to do?


I assume that you're iterating over a list of lists or tuples, and the second item is the one you're interested in - that's at least what you're dealing with now. (but there have to be exactly two items in each list or tuple for the first and the second to mean the same thing)
If I make this assumption, I think they both work okay, assuming that there's exactly one item in the list matching "name". But what do you do if there's two items matching?

(also, why the triple-quote following the first chunk of code? typo?)




Quote

It's also probably been explained in my lectures and I could probably read a lot about it, but what's the convention on commenting? I've seen my lecturer do something like I've been doing:

""" <Description of function>

function(type, type) -> type
"""


Anything else I should know of?



Code comments, as always, should explain only what is not clear from reading your code. And if anything's not clear in your code, you should first look to fix that by clarifying the code. If you have to explain what a variable is for, change its name to make the purpose clear. If its not obvious what a line is intended to do, you should probably change the line to make its intention clear.
Sometimes you'll want to comment on why you're taking a particular step - this can be justified, but it seems to me that it indicates a bit of a failure of writing, and probably suggests that the function in question is too long. A good function does one simple thing, so if there's something that needs to be explained, it often means that you need to spit out that part to another function and give that a good name. In general, a name is much better documentation of your purpose than a comment.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • Entwickler
  • member icon

Reputation: 6602
  • View blog
  • Posts: 26,902
  • Joined: 12-December 12

Re: Removing items from a list with enumerate

Posted 28 March 2014 - 07:39 AM

String literals, most often using triple-quotes, are used to create docstrings:

Quote

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object.

PEP 257 -- Docstring Conventions
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1