> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Sort Array By Parity Python Solution

> Tested Python solution for LeetCode 905 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 905, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/sort-array-by-parity/description/).

Generate this problem as a practice environment: tested reference solution, 14 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 905   # by problem number
lcpy gen -s sort_array_by_parity   # by problem name
```

## Problem

Given an integer array \<code>nums\</code>, move all the even integers at the beginning of the array followed by all the odd integers.\</p>

\<p>Return \<em>any array that satisfies this condition\</em>.\</p>

### Examples

```
Input: nums = [3,1,2,4]
Output: [2,4,3,1]
Explanation: The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
```

```
Input: nums = [0]
Output: [0]
```

### Constraints

* 1 \<= nums.length \<= 5000
* 0 \<= nums\[i] \<= 5000

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/sort_array_by_parity/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/sort_array_by_parity/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def sort_array_by_parity(self, nums: list[int]) -> list[int]:
        result = [num for num in nums if num % 2 == 0]
        result.extend(num for num in nums if num % 2 == 1)
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.