+10 XP

List Comprehensions

A list comprehension builds a new list in one line: [expression for item in iterable]. It's the Pythonic way to transform every element of a list.

Regular loop vs comprehension:
`python
# Regular loop
firing_rates = []
for count in spike_counts:
firing_rates.append(count / 0.15)

# List comprehension — same result, one line
firing_rates = [count / 0.15 for count in spike_counts]
`

python
import numpy as np
spike_counts = [12, 15, 11, 14, 13, 10, 16, 12, 14, 11]
t_max = 0.15  # 150 ms = 0.15 s
# Convert to firing rate (Hz = spikes per second)
firing_rates = [count / t_max for count in spike_counts]
List comprehension: apply count/t_max to every element
print(f"Spike counts:  {spike_counts}")
print(f"Firing rates:  {[round(r, 1) for r in firing_rates]} Hz")
Nested comprehension: round every element of firing_rates
print(f"Mean rate: {np.mean(firing_rates):.1f} Hz")
print(f"Std rate:  {np.std(firing_rates):.1f} Hz")

80–100 Hz is typical for a LIF neuron with this input — consistent with real cortical firing rates.