0

I have the following code to create an array:

    for i in range(channel_count):
        if not result[i] is None:
            result[i] = array('f', result[i])

    return result

Is there any other faster way to create arrays in python 2.7, so it takes less time to execute the code.

Gооd_Mаn
  • 343
  • 1
  • 16

2 Answers2

0

Just define an empty list and use list's append function to append the result to it when the condition holds true

result_arr = []
for i in range(channel_count):
    if not result[i] is None:
        result_arr.append(result[i])
return result

Or list comprehension can be used

result_arr = [r for r in result if not r is None] 

Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
0

You can do it by comprehensive way by follow.

output_array = [c if not c is None for c in range(channel_count)]

You can find more information on List comrehension from here

Devang Padhiyar
  • 3,427
  • 2
  • 22
  • 42