6

I want to know if a list contains only one element, without using len.

What is the most pythonic way to do it between these two solutions? Or maybe none of these are pythonic, and if so what is then?

Solution a: remove item at position 1 and except an IndexError so I know there was only 1 item.

try: a_list.pop(1): except IndexError: #blah 

solution b: use slicing to remove first element and check if the list is now empty

if not a_list[1:]: # blah 
7
  • 4
    if len(a_list) != 1: Commented Jan 30, 2017 at 1:27
  • 1
    if len(a_list) == 1 Commented Jan 30, 2017 at 1:29
  • 3
    In addition to being needlessly convoluted, your first method mutates the list in the process of testing it. Commented Jan 30, 2017 at 1:37
  • @JohnColeman, I forgot to mention that I try to implement a recursive version of len to practice recursion, and therefore I dont want to use len. My bad. I dont care if I mutates the list as I only return 1 if the test fail. Commented Jan 30, 2017 at 1:41
  • 1
    @vildric If you mutate the list in the function you will mutate the list you passed in. So if you're going to create a function that tests if a list contains one element you most likely don't want it to mutate the list. Commented Jan 30, 2017 at 2:05

2 Answers 2

9

One of Python's philosophies is, asking for forgiveness is easier than asking for permission (Python glossary).

In this sense, the most Pythonic way to check if a list has at least one element would be trying and access the element at index 0.

try: some_list[0] except IndexError: print("some_list is empty"). 

Note that pop mutates the list, which is not what you want in a test.

Now if you want to check if a list has exactly one element, a succession of try and except could do the job, but it would become unreadable.

The best way is probably a direct len check:

if len(some_list) == 1: print("some_list has exactly one element") 

You commented that you did not want to use len.

I suggest you use the succession of try and except that I evoked above:

try: l[0] except IndexError: print("length = 0") else: try: l[1] except IndexError: print("length = 1") else: print("length > 1") 
Sign up to request clarification or add additional context in comments.

6 Comments

I forgot to mention that I dont want to use len because I implement a recursive version of it to practice recursion, so I'll use a try/except... Thanks.
@vildric I edited your question to highlight this will. Besides, check my edit for an example.
some_list.pop() will mutate the list. Probably not what you want to do in this situation.
Instead of using pop you could just try accessing the first element and then the second. That way you don't mutate the list. Also, make sure to explicitly except only IndexError. If someone would pass in an integer, boolean or any other type that doesn't define pop, into this test it would print length = 0, which might be unwanted in some cases.
@AndrewGuy That's right, I just kept the same idea as OP. Still, I changed my answer according to this.
|
1

You don't want to mutate the list in place (eg by using .pop()). To check that the list contains only one element, you could use a couple of try, except statements to check that (1) you can access the first element, and (2) you can't access a second element.

def is_single_item_list(list_to_check): #Check that list is not empty try: _ = list_to_check[0] except IndexError: return False #Return true if list has a single element try: _ = list_to_check[1] except IndexError: return True #Return False if more than one element return False 

Really, the most Pythonic way is to just use len.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.