Common Collection Methods in Python

A quick-reference guide to the most-used methods across Python lists, tuples, sets, and dictionaries. Learn which methods each type shares and which are unique.

5 min read

Common collection methods in Python are the built-in methods that perform the most common operations on each collection type. Knowing which methods exist, without having to look them up every time, is part of the fluency that separates beginners from experienced Python programmers. This article is a cross-collection reference that organizes the methods by what they do rather than by type, so you can see at a glance which collections support which operations.

The previous articles in this section covered each method in its native context: list methods in the article on creating and accessing Python lists and the one on modifying Python lists, set methods in the articles on adding and removing set items and set operations, and dictionary methods in accessing and updating dictionaries. This article brings them together for comparison.

Adding items

Every mutable collection type supports adding items, but the method names and behaviors differ because each type organizes data differently. Lists offer append to add a single item to the end, insert to place an item at a specific index, and extend to add every item from an iterable. Sets offer add for a single element and update for every element from an iterable. Dictionaries use square-bracket assignment for single entries and the update method for batch additions from another mapping or an iterable of pairs.

The pattern to remember is that append is list-only, add is set-only, and square-bracket assignment works for both lists and dictionaries but with different meanings: list assignment replaces an item at an index, while dictionary assignment sets the value for a key, creating the key if it does not exist.

Removing items

Lists offer the richest removal API: pop removes and returns an item by index, remove deletes the first occurrence of a value, the del statement removes by index or slice, and clear empties the entire list. Sets offer remove, which raises a KeyError if the element is absent, discard, which silently does nothing if the element is absent, pop, which removes and returns an arbitrary element, and clear. Dictionaries offer pop to remove a key and return its value, popitem to remove and return the most recently inserted pair, the del statement for key-based deletion, and clear.

The remove versus discard distinction on sets is the most important behavioral difference to remember. Remove signals that absence is an error; discard signals that absence is normal. This same distinction appears in dictionary operations: square-bracket access raises a KeyError for missing keys, while the get method returns a default.

Looking up and testing

All collection types support the in operator for membership testing, though the performance varies from constant time for sets and dictionaries to linear time for lists and tuples. Lists and tuples provide the index method to find the position of a value and the count method to count occurrences. Sets rely on the in operator for existence checking because sets have no positions. Dictionaries provide get for safe value retrieval with a default, and setdefault for the combined check-and-set operation.

The count method on lists and tuples returns the number of times a value appears by scanning the entire sequence. The same operation on a set would always return either 0 or 1 because sets eliminate duplicates. This is why sets do not have a count method: the answer is always trivially determined by the in operator.

pythonpython
colors = ["red", "blue", "red", "green"]
print(colors.count("red"))
print(colors.index("green"))

This prints 2 for the count and 3 for the index, the position of the first match. The same .count("red") call on a set built from this list would fail, since sets have no count method at all.

Transforming and copying

Lists are the only collection type with built-in sort and reverse methods, because lists are the only type where order is both meaningful and mutable. Tuples cannot be sorted or reversed in place because they are immutable. Sets cannot be sorted or reversed because they have no order. Dictionaries preserve insertion order and can be iterated in reverse since Python 3.8, but they do not have sort or reverse methods.

All three mutable types, lists, sets, and dictionaries, provide a copy method that creates a shallow copy. Lists also support slicing for copying, and all three types can be passed to their own constructor to create a copy. The distinction between shallow and deep copies is covered in the article on shallow vs deep copy in Python collections, which applies the concepts from copying lists to dictionaries and sets as well.

Rune AI

Rune AI

Key Insights

  • Lists have the most methods: append, extend, insert, remove, pop, clear, index, count, sort, reverse, copy.
  • Dictionaries provide get, pop, popitem, update, setdefault, keys, values, items, and copy.
  • Sets provide add, update, remove, discard, pop, clear, and set-operation methods like union and intersection.
  • Tuples have only count and index, reflecting their role as read-only records.
  • Methods that modify in place return None across all mutable collection types.
RunePowered by Rune AI

Frequently Asked Questions

Which collection type has the most methods?

Lists have the most methods, over a dozen including append, extend, insert, remove, pop, clear, index, count, sort, reverse, and copy. Dictionaries come second with methods like get, pop, popitem, update, setdefault, keys, values, and items. Sets have a focused set of about a dozen methods. Tuples have only two: count and index.

Do all collections have a copy method?

Lists, sets, and dictionaries all have a copy method. Tuples do not because they are immutable and do not need copying. The copy method on all three mutable types creates a shallow copy of the collection.

What is the setdefault method on dictionaries?

The setdefault method checks whether a key exists in the dictionary. If it does, the method returns its value. If it does not, the method inserts the key with a provided default value and returns that default. It is a combined check-and-set operation that replaces the pattern of using an if statement with in before assigning a default.

Conclusion

Knowing which methods are available on which collection type is practical knowledge that saves you from reaching for the documentation every few minutes. Lists have the richest API, dictionaries are a close second, sets are purposefully minimal, and tuples are deliberately sparse.