Python Data Structure
Data Structure
Set
HashMap
OrderedDict
hashmap + doubly-linked list
List
nums.sort(): Sorts the list in-place.O(nlogn)sorted(nums): Returns a new list that is sorted.
[x + y for x in nums]: iterating through a nested list.O(n)nums.insert(index, x): Insertsxat the specifiedindexin the list.O(n)in the worst case.nums.index(x):Returns the first index of the elementxin the list.O(n)nums.remove(x): Removes the first occurrence ofxfrom the list.O(n)in the worst case.If
xis not in the list, it raises aValueError.
nums.pop(index): Removes and returns the element at the specifiedindex.O(n)nums.pop(): If no index is specified, it removes and returns the last item.O(1)
nums.clear(): Removes all elements from the list.O(n)nums[::-1]: ReverseO(n)nums.count(x): Returns the number of occurrences ofxin the list.O(n)zip(list1, list2): Combines two (or more) iterables (like lists) into an iterator of tuples, where thei-th tuple contains thei-th element from each iterable.O(min(n,m))map(func, iterable): Applies a functionfuncto each item of the iterable and returns an iterator yielding the results.O(n)filter(func, iterable): Filters elements from the iterable wherefuncreturnsTrueand returns an iterator of the filtered results.O(n)
Stack
Deque
Heap
Your own Comparison
Use Tuple
Wrap objects and implement
__lt__Use
functools.cmp_to_keyinside a wrapper object
Last updated