← Back to Tutorials

Python List Methods: Complete Reference with Examples

Every built-in Python list method explained — signature, parameters, return value, and a runnable example you can execute in the browser.

Quick Index
<a href="#append">append()</a><a href="#extend">extend()</a><a href="#insert">insert()</a><a href="#remove">remove()</a><a href="#pop">pop()</a><a href="#clear">clear()</a><a href="#index">index()</a><a href="#count">count()</a><a href="#sort">sort()</a><a href="#reverse">reverse()</a><a href="#copy">copy()</a>
<div><div><div><div id="append"><span>append()</span></div></div> <div><p>Add a single item x to the end of the list. Mutates the list in place.</p> <div><div><div>Signature</div> <code>list.append(x)</code></div> <div><div>Parameters</div> <p>x — any object to append as one element.</p></div> <div><div>Returns</div> <p>None (the list is modified in place).</p></div></div> <div><pre>nums = [1, 2, 3] nums.append(4) print(nums) # [1, 2, 3, 4] # append adds one element — even if it's a list nums.append([5, 6]) print(nums) # [1, 2, 3, 4, [5, 6]]</pre></div> <span><a>Try this example</a></span></div></div></div><div><div><div><div id="extend"><span>extend()</span></div></div> <div><p>Extend the list by appending every item from an iterable. Use this instead of append when you want to merge sequences.</p> <div><div><div>Signature</div> <code>list.extend(iterable)</code></div> <div><div>Parameters</div> <p>iterable — any iterable (list, tuple, set, generator, string).</p></div> <div><div>Returns</div> <p>None.</p></div></div> <div><pre>nums = [1, 2, 3] nums.extend([4, 5, 6]) print(nums) # [1, 2, 3, 4, 5, 6] # strings are iterable too letters = ['a'] letters.extend("bcd") print(letters) # ['a', 'b', 'c', 'd']</pre></div> <span><a>Try this example</a></span></div></div></div><div><div><div><div id="insert"><span>insert()</span></div></div> <div><p>Insert item x at position i, shifting existing items to the right.</p> <div><div><div>Signature</div> <code>list.insert(i, x)</code></div> <div><div>Parameters</div> <p>i — index to insert before. x — item to insert.</p></div> <div><div>Returns</div> <p>None.</p></div></div> <div><pre>nums = [1, 2, 4] nums.insert(2, 3) print(nums) # [1, 2, 3, 4] # insert at the beginning nums.insert(0, 0) print(nums) # [0, 1, 2, 3, 4]</pre></div> <span><a>Try this example</a></span></div></div></div><div><div><div><div id="remove"><span>remove()</span></div></div> <div><p>Remove the first occurrence of x. Raises ValueError if x is not present.</p> <div><div><div>Signature</div> <code>list.remove(x)</code></div> <div><div>Parameters</div> <p>x — value to remove.</p></div> <div><div>Returns</div> <p>None.</p></div></div> <div><pre>fruits = ["apple", "banana", "apple", "cherry"] fruits.remove("apple") print(fruits) # ['banana', 'apple', 'cherry'] # guard against ValueError if "kiwi" in fruits: fruits.remove("kiwi")</pre></div> <span><a>Try this example</a></span></div></div></div><div><div><div><div id="pop"><span>pop()</span></div></div> <div><p>Remove and return the item at position i. Defaults to the last item, making pop() an O(1) stack operation.</p> <div><div><div>Signature</div> <code>list.pop([i])</code></div> <div><div>Parameters</div> <p>i — optional index (default -1).</p></div> <div><div>Returns</div> <p>The removed item. Raises IndexError if the list is empty or i is out of range.</p></div></div> <div><pre>stack = [1, 2, 3, 4] last = stack.pop() print(last) # 4 print(stack) # [1, 2, 3] first = stack.pop(0) print(first) # 1</pre></div> <span><a>Try this example</a></span></div></div></div><div><div><div><div id="clear"><span>clear()</span></div></div> <div><p>Remove every item from the list. Equivalent to del list[:].</p> <div><div><div>Signature</div> <code>list.clear()</code></div> <div><div>Parameters</div> <p>None.</p></div> <div><div>Returns</div> <p>None.</p></div></div> <div><pre>items = [1, 2, 3] items.clear() print(items) # []</pre></div> <span><a>Try this example</a></span></div></div></div><div><div><div><div id="index"><span>index()</span></div></div> <div><p>Return the zero-based index of the first item equal to x. Raises ValueError if not found.</p> <div><div><div>Signature</div> <code>list.index(x[, start[, end]])</code></div> <div><div>Parameters</div> <p>x — value to find. start / end — optional slice bounds.</p></div> <div><div>Returns</div> <p>int — the index of the first match.</p></div></div> <div><pre>letters = ['a', 'b', 'c', 'b'] print(letters.index('b')) # 1 print(letters.index('b', 2)) # 3 (search from index 2)</pre></div> <span><a>Try this example</a></span></div></div></div><div><div><div><div id="count"><span>count()</span></div></div> <div><p>Return how many times x appears in the list.</p> <div><div><div>Signature</div> <code>list.count(x)</code></div> <div><div>Parameters</div> <p>x — value to count.</p></div> <div><div>Returns</div> <p>int.</p></div></div> <div><pre>nums = [1, 2, 2, 3, 2, 4] print(nums.count(2)) # 3 print(nums.count(9)) # 0</pre></div> <span><a>Try this example</a></span></div></div></div><div><div><div><div id="sort"><span>sort()</span></div></div> <div><p>Sort the list in place. Stable and uses TimSort. For a sorted copy without mutating, use sorted(list).</p> <div><div><div>Signature</div> <code>list.sort(*, key=None, reverse=False)</code></div> <div><div>Parameters</div> <p>key — function applied to each element for comparison. reverse — sort descending when True.</p></div> <div><div>Returns</div> <p>None.</p></div></div> <div><pre>nums = [3, 1, 4, 1, 5, 9, 2, 6] nums.sort() print(nums) # [1, 1, 2, 3, 4, 5, 6, 9] words = ["banana", "apple", "cherry"] words.sort(key=len, reverse=True) print(words) # ['banana', 'cherry', 'apple']</pre></div> <span><a>Try this example</a></span></div></div></div><div><div><div><div id="reverse"><span>reverse()</span></div></div> <div><p>Reverse the elements of the list in place.</p> <div><div><div>Signature</div> <code>list.reverse()</code></div> <div><div>Parameters</div> <p>None.</p></div> <div><div>Returns</div> <p>None.</p></div></div> <div><pre>nums = [1, 2, 3, 4] nums.reverse() print(nums) # [4, 3, 2, 1] # for an iterator that doesn't mutate, use reversed() for n in reversed([1, 2, 3]): print(n)</pre></div> <span><a>Try this example</a></span></div></div></div><div><div><div><div id="copy"><span>copy()</span></div></div> <div><p>Return a shallow copy of the list. Equivalent to list[:] or list(original).</p> <div><div><div>Signature</div> <code>list.copy()</code></div> <div><div>Parameters</div> <p>None.</p></div> <div><div>Returns</div> <p>A new list containing the same references.</p></div></div> <div><pre>original = [1, 2, [3, 4]] shallow = original.copy() shallow.append(99) print(original) # [1, 2, [3, 4]] print(shallow) # [1, 2, [3, 4], 99] # shallow copies share nested objects shallow[2].append("shared!") print(original) # [1, 2, [3, 4, 'shared!']]</pre></div> <span><a>Try this example</a></span></div></div></div>

Practice Python Online

Open the online Python editor, paste any example, and see the output instantly — no installs.