Python Forum
code pattern to test if list has all the same
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code pattern to test if list has all the same
#6
(Jul-25-2018, 04:10 AM)Skaperen Wrote: all handles an empty list by returning True, which would be the wrong thing in my current projects.
foo = []
if foo and all(isinstance(x, float) for x in foo):
    print('all float')
else:
    print('some problem')
Output:
some problem
just for completeness - there is also any() - Return True if any element of the iterable is true. If the iterable is empty, return False.
also you can 'extend' all() it in your own helper function
def nonempty_alltypes(iterable, check_types):
    """Check that iterable is not empty and all elements are one of check_types. Returns bool."""

    return all((iterable, all(isinstance(x, check_types) for x in iterable)))


test_list = [[1,2], [], [1, 3, 'a']]
test_for = int

for test_item in test_list:
    print('{} | {} --> {}'.format(test_item, test_for, nonempty_alltypes(test_item, test_for)))

test_item = [1, 2, 4.5]
test_for = (int, float)
print('{} | {} --> {}'.format(test_item, test_for, nonempty_alltypes(test_item, test_for)))
Output:
[1, 2] | <class 'int'> --> True [] | <class 'int'> --> False [1, 3, 'a'] | <class 'int'> --> False [1, 2, 4.5] | (<class 'int'>, <class 'float'>) --> True
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: noobs: code pattern to test if list has all the same - by buran - Jul-25-2018, 06:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,159 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  can someone test the code for me kucingkembar 4 1,555 Feb-17-2022, 10:22 AM
Last Post: kucingkembar
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,064 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  How to run a pytest test for each item in a list arielma 0 2,324 Jan-06-2021, 10:40 PM
Last Post: arielma
  Remove specific elements from list with a pattern Xalagy 3 2,623 Oct-11-2020, 07:18 AM
Last Post: Xalagy
  How to write test cases for a init function by Unit test in python? binhduonggttn 2 3,062 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 2,511 Feb-18-2020, 08:03 AM
Last Post: binhduonggttn
  test pattern and add result in a table sam1975 1 1,867 Mar-05-2019, 02:41 PM
Last Post: sam1975
  modify line in file if pattern found in list. kttan 1 2,186 Dec-10-2018, 08:45 AM
Last Post: Gribouillis
  create list from repeated pattern in python Code4fun 2 3,394 Sep-25-2018, 07:09 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020