-2

Is it possible for me to do the following, and if so, what am I doing wrong?

I want to grab the last item in a list, and rely on .find() to return the last item, even if the list is empty.

I have a list like this:

list = ["item1", "item2", "item3", "etc"]

and I want to get the last element even if it's empty. To do that, I access the last element using list[-1:]. My problem is that I believe I need to use the .find() on the last element of that list. However, if the list is empty, .find() does not work.

The reason that won't work is because list[-1] will return etc, but list[-1:] will return ['etc']. Any other ways to use .find() in this situation?

octopusgrabbus
  • 10,250
  • 13
  • 64
  • 126
D.Joe
  • 57
  • 9
  • 2
    If it is empty, how can a list _have_ a last element? – Akshat Mahajan Apr 06 '16 at 17:55
  • It would be much better to rewrite your code so that you can _check_ if the list is empty using simple if/else statements and do something different. If your whole plan revolves in doing `.find()` on a non-existent item, you're going to have major problems :) – Akshat Mahajan Apr 06 '16 at 17:57
  • You want to use `find` on an element that doesn't exist. What do you even want that to do? Would you expect to be able to drive the last car in an empty garage? Eat the last potato chip in an empty bag? The thing you're trying to do is not meaningful; you need to detect the emptiness of the list and do something else. – user2357112 Apr 06 '16 at 18:01
  • I'm not sure this is a bad question, only that the OP assumes some stuff about Python lists that almost sounds like C. – octopusgrabbus Apr 06 '16 at 18:03
  • @AkshatMahajan I was using the .find() inside an if statement, so if i used the find on the empty list, the if statement wouldn't run. I fixed my code with `if len(list) > 0 and list[-1:].find("stuff") != -1: #do something` – D.Joe Apr 06 '16 at 18:07
  • Are you trying to do something like `if list and list[-1] == 'something'`? – Wayne Werner Apr 06 '16 at 18:55
  • I edited this post, because I am guessing the OP was asked in an attempt to cut down on using conditionals, but instead relying on properties of an empty list and the `.find()` method. I'm not sure. In a way, at least to me, the OP follows some of Python's minimalist constructs. Perhaps I'm wrong. – octopusgrabbus Apr 07 '16 at 12:42
  • I personally don't think this OP should have been voted down 3 times. The wording was awkward, but, at least to me, an interesting question touching a way of constructing Python programs was asked. – octopusgrabbus Apr 07 '16 at 13:28

1 Answers1

0

Why can't you test to see if there's something in the list, and then use find?

>>> list = []
>>> if list:
        #You would use find here.
...     print("not empty")
... else:
...     print("empty")
... 
empty
>>>
octopusgrabbus
  • 10,250
  • 13
  • 64
  • 126
  • I actually fixed it with if len(list) > 0, but your code works too – D.Joe Apr 06 '16 at 18:05
  • @D.Joe I don't know what a purist would say. Calling a function `len` might be more expensive than using `if list`, but if you're code works, and you understand it, I'd say either way is good. – octopusgrabbus Apr 06 '16 at 18:08
  • @D.Joe: It's better to use `if list:`. See [here](https://stackoverflow.com/questions/53513/best-way-to-check-if-a-list-is-empty). – zondo Apr 07 '16 at 12:32
  • @zondo I am noticing that as Python moves more and more into mainstream use, the Python way as an absolute way to do things is falling out of favor. – octopusgrabbus Apr 07 '16 at 12:44
  • @octopusgrabbus: Must we bow to the non-programmers who are moving in? Shouldn't we set a good example? – zondo Apr 07 '16 at 13:11
  • @zondo I agree with you, because performing some of the best Python ways allows for better performance, like using list iteration instead of a `for` loop. However, in the case of this question, I appreciate the fact that it was thought that `.find()` would handle all cases. – octopusgrabbus Apr 07 '16 at 13:26