-1

My function always returns None, what is going on here?

Az= [5,4,25.2,685.8,435,2,8,89.3,3,794]
new = []

def azimuth(a,b,c):
 if c == list:
    for i in c:
       if i > a and i < b:
           new.append(i)
           return new

d=azimuth(10,300,Az)
print d

In addition, if someone know how can i extract the location of these numbers to a different list it will be really helpful.

nadav_n
  • 45
  • 1
  • 11
  • to know the location of the a item inside a list you can use `Az.index(item)` that give you the position of the first occurrence of item inside of your list, and if you want the positions of all, you can use `enumerate(Az)` that return a list of tuples `(position,item)` – Copperfield Dec 19 '15 at 20:37

2 Answers2

5

if c == list: is checking if c is a type i.e a list also if if i > a and i < b: never evaluates to True you will never reach your return statement therefore by default return None as all python functions do that don't specify a return value, I imagine you want something like:

Az = [5,4,25.2,685.8,435,2,8,89.3,3,794]

def azimuth(a,b,c):
  new = []
  if isinstance(c ,list):
     for i in c:
        if  a < i < b:
           new.append(i)
  return new # return outside the loop unless you only want the first 

Which can be simplified to:

def azimuth(a, b, c):
    if isinstance(c, list):
        return [i for i in c if a < i < b]
    return [] # if  c is not a list return empty list

If you want the index too use enumerate:

def azimuth(a, b, c):
    if isinstance(c, list):
        return [(ind,i) for i, ind in enumerate(c) if a < i < b]
    return []

If you want them separately:

def azimuth(a,b,c):
  inds, new = [], []
  if isinstance(c ,list):
     for ind, i in enumerate(c):
        if  a < i < b:
           new.append(i)
           inds.append(ind)
  return new,inds # 

Then unpack:

new, inds = azimuth(10, 300, Az)
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • 1
    you could also make other simplification `i > a and i < b` to `a < i < b` – Copperfield Dec 19 '15 at 20:32
  • @Copperfield, True, thanks simplified chained comparison is nicer – Padraic Cunningham Dec 19 '15 at 20:34
  • that's great, i did run the first code (before the simplified). After running in with 'Az' as the list, it it returns only 1 variable in the new list- 25.2, but it was also suppose to put inside 89.3. ( i did a and b as 10 and 300) append can insert only 1 variable to the 'new' list? @PadraicCunningham – nadav_n Dec 19 '15 at 20:46
  • `print(azimuth(10, 300, Az))` returns `[25.2, 89.3]` which is correct, there are the only two values in the range – Padraic Cunningham Dec 19 '15 at 20:50
  • that is the problem, i don't understand why it returns only 1 value, i switched them and then i get a return of only 89.3, as it appends only the 1st variable that fits the conditions. maybe it has to do with my editor, i know the solution should be clear and that both values should return @PadraicCunningham – nadav_n Dec 19 '15 at 21:26
  • @nadav_n, You can see here https://repl.it/B9jV/0 that is does exactly what it should – Padraic Cunningham Dec 19 '15 at 21:38
  • i'm such a noob, no wonder people are killing my rep, thanks man. @PadraicCunningham – nadav_n Dec 19 '15 at 21:40
2

The first if in the function is checking whether c is the built in type list.

>>> list
<type 'list'>

Thus the check won't be true and return new will never be reached. The function will return the default value None in this case.

To check whether something is a list, use isinstance:

>>> c = [1,2,3]
>>> c == list
False
>>> isinstance(c, list)
True
timgeb
  • 76,762
  • 20
  • 123
  • 145