0
def filter_list(elements):
    data = [elements]
    for a in elements:
        if a == (int(a) or float(a)) and a >= 1 and a < 50:
            return "true"
        else:
            return "false"

filter_list([1, 2, 3])
filter_list([0, 2, 3])
filter_list([1, 50, 3])

This function searches if int or floats between 1 and 50 are in the list. But it only searches the first list entry. How can i expand the search on the whole list? Also if i write 1.1 in the list, the result will be False.

filter_list([1, 2, 3]) = True
filter_list([0, 2, 3]) = False
filter_list([1, 50, 3]) = True (which should be False)
filter_list([1.1, 2, 3]) = False (which should be True)

Edited:

def filter_list(elements):
    data = [elements]
    for a in elements:
        if a == int(a) and a >= 1 and a < 50:
            filter = []
            filter.append(a)
    return filter
filter_list([2, 1, 4, 5, 6])

This results in [6], which i dont want to.

  • 1
    During the first iteration of the loop, you will **always** return from the function: without iterating over the rest of the list. Can you see why? – hnefatl Nov 04 '18 at 17:32
  • 1
    You return from the loop after checking the very first element, what else could happen? – tevemadar Nov 04 '18 at 17:33
  • For the mysterious part: `int(1.1) or float(1.1)` results in `1`, which is not equal to `1.1`. So it is not a check for "a is an int or float number?" if that is the intent. – tevemadar Nov 04 '18 at 17:37
  • So instead of returning, im now creating a empty list called filter. I append a to it. Now only the last digit from the list data is appended to it. Thats not what i want. I will edit the question. – Marcel Hlmn Nov 04 '18 at 17:41
  • Should be `return True` or `return False` -- NOT `return "false"` which in Python would be `True` – dawg Nov 04 '18 at 18:03

2 Answers2

0

You return "false" too early, as you always return from the first iteration. Also, your type check is not a type check at all and will raise errors for most arbitrary inputs. Use isinstance instead. Moreover, Python allows chained comparisons. You can do e.g.

def filter_list(elements):
    for a in elements:
        if isinstance(a, (int, float)) and 1 <= a < 50:
            return True  # probably, you want to return a bool value
    # only now that you have checked all elements, you can know for sure
    return False  

Or shorter, using any:

def filter_list(elements):
    return any(isinstance(a, (int, float)) and 1 <= a < 50 for a in elements)

For compiling a new list of matching values, do:

def filter_list(elements):
    # instantiate the new list before the loop
    fil = []  # do not shadow the built-in filter function
    for a in elements:
        if isinstance(a, int) and 1 <= a < 50:
            fil.append(a)
    return fil

Or shorter, using a list comprehension:

def filter_list(elements):
    return [a for a in elements if isinstance(a, int) and 1 <= a < 50]
user2390182
  • 72,016
  • 6
  • 67
  • 89
-1

Because of the name of your function, I will suggest another approach: using reduce.

Basically, you have to implement a function, returning boolean on one single item. Afterwards, use reduce to apply this function to each element of your list.

f = lambda x : 1.0 <= x < 50

# Is equivalent to
def f(item):
  return 1.0 <= item < 50

Then apply to your list:

print filter(f, [0,1,2])
[1, 2]

Applied to your problem, you should ensure that the size of the filtered list is the same than the original list (or modify the oracle function to do the opposite, and ensure the result is empty).

def check(lst):
      return len(lst) == len(filter(f, lst))

print check([0,1,2])
# Outputs False
print check([1,2,3])
# Outputs True

Also, "True" and "False" are typed string, whereas you want them to be booleans. Hence True or False.

Aif
  • 11,015
  • 1
  • 30
  • 44