0

I would like to know what am I doing wrong with my code. I'm a JavaScript developer & I'm learning Python currently. I'm creating a function that takes a list as an argument, loops through it & appends a new list with items type string from the previous one. However, I'm getting this error SyntaxError: bad input on line 4 in main.py.

First, I would like to know what I'm doing wrong. Second, how can I fix it?

def filter_list(arr):
  results = list()

    for x in arr:
        if isinstance(x, str):
        results.append(x)
        print(results)

filter_list([1, 2, 3, "a", "b", 4])
Manuel Abascal
  • 5,616
  • 5
  • 35
  • 68
  • 1
    The indentation in your example doesn't look right. – gilch Apr 25 '20 at 05:57
  • Python uses indentation in place of `{}` structures in JS, so it needs to be precise. Try https://rextester.com/PYNCJY90704 – Nick Apr 25 '20 at 05:58
  • Does this answer your question? [I'm getting an IndentationError. How do I fix it?](https://stackoverflow.com/questions/45621722/im-getting-an-indentationerror-how-do-i-fix-it) – Nick Apr 25 '20 at 05:59
  • @Nick you're totally right, it works fine with link you provided it But somehow on this playground, it doesn't https://trinket.io/python/2578572a61 – Manuel Abascal Apr 25 '20 at 06:02
  • That link has different indentation though. – gilch Apr 25 '20 at 06:03
  • @gilch ok, thanks! At least now, I know that it was an error in my code or logic. If you add answer, I'll accept it so you get your rep! Thanks again! – Manuel Abascal Apr 25 '20 at 06:04

4 Answers4

1

As others have mentioned, your issue is with the indentation in your if statement. If your goal is to obtain a filtered list of only strings from the original list, not just printing the list you could go with:

def filter_list(arr):
    results = list()

    for x in arr:
        if isinstance(x, str):
            results.append(x)
    return results

Another alternative would be to use a functional approach that is popular in python and probably familiar coming from javascript:

results = filter(lambda x: isinstance(x, str), arr)

In the above, results will be an iterator as filter is a generator function, a function for which the results can be iterated over. If you want to get back a list, you can add list(results)

MarkAWard
  • 1,699
  • 2
  • 16
  • 28
0
def filter_list(arr):

  results = list()

  for x in arr:
      if isinstance(x, str):
          results.append(x)
          print(results)

filter_list([1, 2, 3, "a", "b", 4])

Your spacing is incorrect. This code works.

Check if statement spacing. Particularly line 5 block.

  • 1 tab indent on line 5
  • 2 tab indents on line 6
  • And 3 on line 7 and 8

Indents are typically 4 spaces, you can use literal tabs but note you cannot mix tabs and spaces

MarkAWard
  • 1,699
  • 2
  • 16
  • 28
nolyoly
  • 116
  • 2
  • 14
0

The indentation is incorrect. Python's blocks begin with a :, but it needs a dedent to know when they end. An empty block must have an explicit pass.

In particular, note that this if has an empty block without a pass. (The next line was not indented more, so the block must have ended.)

if isinstance(x, str):
results.append(x)

And also, within a block, indentation must be consistent. The for is an unexpected indent, because there was no starting : to allow it to be indented more.

results = list()

    for x in arr:
gilch
  • 10,813
  • 1
  • 23
  • 28
  • consider providing the code that is correctly indented along with pointing out the lines where there is an error – MarkAWard Apr 25 '20 at 06:26
0
def filter_list(arr):
    results = list()

    for x in arr:
        if isinstance(x, str):
            results.append(x)
        print(results)

filter_list([1, 2, 3, "a", "b", 4])

1) Make the indentaion same in line 2 and line 4. 2) indent result.append(x) after if statement.

darclander
  • 1,526
  • 1
  • 13
  • 35