Your ultimate guide

All functions in List, Tuple, Set and Dict from Python

Most commonly used functions in List(mutable) and Tuple(immutable):
Lists and tuples are similar, but lists are mutable while tuples are immutable. 
Function [List] (Tuple)
len(list/Tuple): Returns the number of elements in the list/Tuple Y Y
append(item): Adds an item to the end of the list. Y N
extend(iterable): Appends the elements of an iterable (e.g., another list) to the end of the list. Y N
insert(index, item): Inserts an item at a specific index. Y N
remove(item): Removes the first occurrence of a specific item. Y N
pop(index): Removes and returns the item at a specific index. If no index is provided, it removes and returns the last item. Y N
index(item): Returns the index of the first occurrence of the specified item. Y Y
count(item): Returns the number of times a specific item appears in the list/tuple. Y Y
sort(): Sorts the list in ascending order. Y N
sort(reverse=True): Sorts the list in descending order. Y N
reverse(): Reverses the order of elements in the --list. Y N
copy(): Returns a shallow copy of the list. Y N
clear(): Removes all items from the list. Y N
min(list/tuple)`: Returns the minimum value in the list/tuple. Y Y
max(list/tuple): Returns the maximum value in the list/tuple. Y Y
sum(list/tuple): Returns the sum of all elements in the list/tuple. Y Y
sorted(iterable): Returns a new sorted list from the elements of an iterable without modifying the original list/tuple. Y Y
any(iterable): Returns True if any element in the iterable is True. Y Y
all(iterable): Returns True if all elements in the iterable are True. Y Y
zip(iterable1, iterable2): Combines two or more iterables into tuples, stopping when the shortest iterable is exhausted. Y Y
enumerate(iterable): Returns an iterator that yields pairs of index and value for each element in the iterable. Y Y
filter(function, iterable): Returns an iterator containing only the elements of the iterable for which the function returns `True`. Y Y



🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴



Most commonly used functions in Set(Mutable) and Frozenset(Immutable):
  • set(iterable): Create a new set from an iterable.
  • frozenset(iterable_name): Create a new frozenset from an iterable(list, set, tuple).
Function {set} (frozenset)
add(element): Adds an element to the set. Y N
update(iterable): Updates the set by adding elements from an iterable. Y N
remove(element): Removes an element from the set. Raises an error if the element is not present. Y N
discard(element): Removes an element from the set if it exists; does not raise an error if the element is not present. Y N
pop(): Removes a random element from the set. Raises an error if the set is empty. Y N
clear()`: Removes all elements from the set. Y N
copy(): Returns a shallow copy of the set/frozenset. Y Y
difference(other): Returns a new set containing elements that are in the current set/frozen but not in the other set/frozenset.
Syntax(x and y are set's): z = x.difference(y)
Y Y
difference_update(other): Removes the elements that exist in both sets from the original set.
Syntax(x and y are set's): x.difference_update(y)
Y N
intersection(other): Returns a new set/frozenset containing elements that are in both the current set/frozenset and the other set/frozenset.
Syntax: z = x.intersection(y)
Y Y
intersection_update(other): Updates the set by keeping only elements that are also in the other set
Syntax: x.intersection_update(y)
Y N
union(other): Returns a new set/frozenset containing all unique elements from both the current set/frozenset and the other set/frozenset.
Syntax: z = x.union(y)
Y Y
update(other): Updates the set by adding elements from another set.
Syntax: x.update(y)
Y N
issubset(other): Returns `True` if the set/frozenset is a subset of the other set/frozenset. Y Y
issuperset(other): Returns `True` if the set/frozenset is a superset of the other set/frozenset. Y Y
isdisjoint(other): Returns `True` if the set/frozenset has no elements in common with the other set/frozenset. Y Y



🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴🛴



The most commonly used functions in the Dictionary(dicts):
Function
### Creating Dictionaries:
dict(): Creates an empty dictionary.
dict(key=value, ...): Creates a dictionary with key-value pairs.
dict(iterable): Creates a dictionary from an iterable of key-value pairs (e.g., a list of tuples).

### Accessing and Modifying Elements:
d[key]: Accesses the value associated with a specific key. Raises a `KeyError` if the key is not present.
d.get(key): Accesses the value associated with a key. Returns `None` if the key is not present (no error).
d.get(key, default): Accesses the value associated with a key, returning the specified default value if the key is not present.
d.keys(): Returns a view of all keys in the dictionary.
d.values(): Returns a view of all values in the dictionary.
d.items(): Returns a view of key-value pairs in the dictionary.
d.pop(key): Removes and returns the value associated with a specific key. Raises a `KeyError` if the key is not present.
d.pop(key, default): Removes and returns the value associated with a key, returning the specified default value if the key is not present.
d.popitem(): Removes and returns an arbitrary key-value pair as a tuple.
d.update(other_dict): Updates the dictionary with key-value pairs from another dictionary.
d.setdefault(key, default): Returns the value associated with a key, or sets it to the default value if the key is not present.

### Dictionary Views:
dict.keys(): Returns a view of keys in the dictionary.
dict.values(): Returns a view of values in the dictionary.
dict.items(): Returns a view of key-value pairs in the dictionary.

### Dictionary Methods:
d.clear(): Removes all key-value pairs from the dictionary.
d.copy(): Returns a shallow copy of the dictionary.
d.fromkeys(iterable, value): Creates a new dictionary with keys from an iterable and values set to a specified value.
d.setdefault(key, default): Returns the value associated with a key, or sets it to the default value if the key is not present.
d.update(other_dict): Updates the dictionary with key-value pairs from another dictionary.

### Other Functions:
len(d): Returns the number of key-value pairs in the dictionary.
sorted(d): Returns a sorted list of keys in the dictionary.
in and not in operators: Check if a key exists in the dictionary.

No comments:

Post a Comment