Python set Methods
Python set Methods
Here's a detailed explanation of various set methods in Python:
1. set() function: This is used to create a new set object. The argument passed should be iterable (can be a list, tuple, dictionary, etc.) and this method will automatically remove any duplicates.
Example:
set1 = set([1, 2, 3, 4, 5])
print(set1) # Output: {1, 2, 3, 4, 5}
2. update() method: This is used to add elements from another set or any iterable to the current set.
Example:
set1 = set([1, 2, 3])
set2 = set([3, 4, 5])
set1.update(set2)
print(set1) # Output: {1, 2, 3, 4, 5}
3. intersect() method: This is used to find the common elements between two sets.
Example:
set1 = set([1, 2, 3, 4, 5])
set2 = set([3, 4, 5, 6, 7])
print(set1.intersection(set2)) # Output: {3, 4, 5}
4. union() method: This is used to combine two sets into one.
Example:
set1 = set([1, 2, 3])
set2 = set([3, 4, 5])
print(set1.union(set2)) # Output: {1, 2, 3, 4, 5}
5. difference() method: This is used to find the elements in one set that are not present in another.
Example:
set1 = set([1, 2, 3])
set2 = set([3, 4, 5])
print(set1.difference(set2)) # Output: {1, 2}
6. symmetric_difference() method: This is used to find the elements that are not common between two sets.
Example:
set1 = set([1, 2, 3])
set2 = set([3, 4, 5])
print(set1.symmetric_difference(set2)) # Output: {1, 2, 4, 5}
7. isdisjoint() method: This is used to check if two sets are disjoint (have no elements in common).
Example:
set1 = set([1, 2, 3])
set2 = set([4, 5, 6])
print(set1.isdisjoint(set2)) # Output: True
8. issubset() method: This is used to check if one set is a subset of another.
Example:
set1 = set([1, 2, 3])
set2 = set([1, 2, 3, 4, 5])
print(set1.issubset(set2)) # Output: True
9. issuperset() method: This is used to check if one set is a superset of another.
Example:
set1 = set([1, 2, 3])
set2 = set([1, 2, 3])
print(set1.issuperset(set2)) # Output: True
10. copy() method: This is used to create a copy of the current set.
Example:
set1 = set([1, 2, 3])
set2 = set1.copy()
print(set1) # Output: {1, 2, 3}
print(set2) # Output: {1, 2, 3} (newly created)
Python dictionary methods
I'll respond in English for this particular question.
Python dictionaries provide several built-in methods that can be used to manipulate and extract data from the dictionary. Here are some of the most commonly used dictionary methods:
keys()
: This method returns a view object that displays all the keys available in the dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.keys()) # Output: dict_keys(['a', 'b', 'c'])
values()
: This method returns a view object that displays all the values available in the dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.values()) # Output: dict_values([1, 2, 3])
items()
: This method returns a view object that displays all the items available in the dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.items()) # Output: dict_items([('a', 1), ('b', 2), ('c', 3)])
get(key[, default])
: This method is used to retrieve the value of a specified key in the dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.get('a')) # Output: 1
print(my_dict.get('d', 'key does not exist')) # Output: key does not exist
setdefault(key[, default])
: This method is used to set the value of a specified key in the dictionary, and if the key does not already exist, it sets the key with a given default value.
my_dict = {'a': 1, 'b': 2}
print(my_dict.setdefault('c', 3)) # Output: 3
print(my_dict) # Output: {'a': 1, 'b': 2, 'c': 3}
pop(key[, default])
: This method is used to remove the item with the specified key and return its value.
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.pop('c')) # Output: 3
print(my_dict) # Output: {'a': 1, 'b': 2}
popitem()
: This method is used to remove and return an arbitrary item (key-value pair) from the dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.popitem()) # Output: ('c', 3)
print(my_dict) # Output: {'a': 1, 'b': 2}
update(other)
: This method is used to update the dictionary with another dictionary.
my_dict = {'a': 1, 'b': 2}
other_dict = {'c': 3, 'd': 4}
print(my_dict.update(other_dict)) # Output: None
print(my_dict) # Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
clear()
: This method is used to remove all items from the dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.clear()) # Output: None
print(my_dict) # Output: {}
fromkeys(seq[, value])
: This method is used to create a new dictionary from the specified sequence of keys and values.
my_list = ['a', 'b', 'c']
print(dict.fromkeys(my_list, 1)) # Output: {'a': 1, 'b': 1, 'c': 1}
Remember that these methods can be used to efficiently manipulate dictionaries and extract the information needed.