0

In order to write my code more elegantly, I would like to append an item to a list and return the index of the newly created item. For example,

a_idx = append(lst, "a")
b_idx = append(lst, "b")
...

To achieve this functionality I wrote a simple append function:

def append(lst, item):
    lst.append(item)
    return len(lst) - 1

Is there something built-in in Python that is similar to my append function?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Alex Shtoff
  • 2,520
  • 1
  • 25
  • 53
  • Nope, there isn't. Why do you need this, really? – Martijn Pieters Jun 28 '15 at 09:57
  • This question is an indication of a suboptimal data structure. I don't think you'll be able to make it elegant. Can you tell us the problem that this would be solving? – TigerhawkT3 Jun 28 '15 at 09:57
  • 2
    If you clarify what you're trying to do more broadly, we may be able to help you find a better overall strategy. Why do you need the indexes/what are you using them for later? Most likely, you can more effectively achieve the same outcome with a different collection type. – Ming Jun 28 '15 at 10:00
  • 1
    @vk1011: no. `append` is a *method* of `list`, and is not a reserved keyword or built-in function: http://stackoverflow.com/questions/22864221/is-the-list-of-python-reserved-words-and-builtins-available-in-a-library – fferri Jun 28 '15 at 10:00
  • @vk1011: there is no such built-in function; there is only the method on a built-in type. The two don't really clash. – Martijn Pieters Jun 28 '15 at 10:01
  • Thanks for correcting me guys. You are right! – vk1011 Jun 28 '15 at 10:01
  • 1
    What do you need the index for? `list.append(element)` adds the given element to the list to be the new last element. You can access the last element of a list using `list[-1]` – albert Jun 28 '15 at 10:04
  • There is no such builtin Alex. What's wrong with your `append(lst, item)` function? Want something more elaborate? Try this: `append = lambda l,i: (lambda ln,l,i: (l.append(i), ln)[1])(len(l), l, i)` (just kidding) – fferri Jun 28 '15 at 10:04
  • @Alex: I think you should give more information about what you are trying to achieve. There isn't a built-in for what you have explained here, but there certainly could be a way to avoid doing this completely if you were to give more information about your problem. For example, what are you doing with the indexes you return? – Samuel O'Malley Jun 28 '15 at 10:05
  • Not a duplicate. I am trying to make it a one-liner. – Alex Shtoff Jun 28 '15 at 10:12
  • In that case, mescalinum's approach will do it for you. You're not saving much though and would make it looking complex. The simplicity you have is pretty 'elegant' already. – vk1011 Jun 28 '15 at 10:15
  • @jonrsharpe, I do not understand why it has been marked as a duplicate. This is not a question about how to get the index of the last added item. This is a question about weather there exists a mechanism in Python which makes it a one-liner (it seems that the answer is NO, according to the comments here), which does not appear in the question linked as "duplicate" – Alex Shtoff Jun 28 '15 at 12:11
  • @Alex if you want an alternative way to achieve the same functionality, consider adding a bounty to the previous question to attract more attention to it. – jonrsharpe Jun 28 '15 at 12:30
  • But the previous question asks something else. I already got an answer to my question, and I also wrote it in the "update", which you deleted. – Alex Shtoff Jun 28 '15 at 14:23

0 Answers0