> For the complete documentation index, see [llms.txt](https://r24zeng.gitbook.io/leetcode-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r24zeng.gitbook.io/leetcode-notebook/zhong-yu-shua-dao-100-dao-le-wo-shi-fen-shui-ling/bu-chong-120-dao/27.-remove-element.md).

# 27. Remove Element

\# Easy

{% hint style="info" %}
Exchange `element = val` with the last element of the list, and pop the last element out.
{% endhint %}

{% tabs %}
{% tab title="Python" %}

```python
class Solution(object):
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        i = 0
        while i < len(nums):
            if nums[i] == val:
                temp = nums[i]
                nums[i] = nums[-1]
                nums[-1] = temp
                nums.pop()
            if i < len(nums) and nums[i] != val:
                i += 1

        return len(nums)
```

{% endtab %}
{% endtabs %}
