Use the iter()
function to get the iterator from the set.
Use the next()
function to get elements from the iterator.
If the collection is empty, specify a default value.
my_set = {'jiyik.com'} element = next(iter(my_set), None) print(element) # ????️ jiyik.com
We use the iter()
function to get the iterator object from the collection.
next()
The function returns the next item from the provided iterator.
This function can pass a default value as the second parameter.
Returns the default value if the iterator is exhausted or empty.
If the iterator is exhausted or empty and no default value is provided, a
StopIteration
exception is raised.
We use None as the default value, but we can use any other value that suits our use case.
my_set = set() element = next(iter(my_set), None) print(element) # ????️ None
The collection is empty, so the default value None is returned.
Alternatively, we can use tuple unpacking.
Use tuple unpacking to get the unique element in a single member set, for example (element,) = my_set
. Tuple unpacking syntax assigns the value of the element to a variable.
my_set = {'jiyik.com'} (element,) = my_set print(element) # ????️ jiyik.com
Please note
that we used a trailing comma after the variable name.
The first element of the iterable object on the right is assigned to the variable on the left.
(element,) = ['a'] print(element) # ????️ 'a'
Alternatively, we can use the list()
class.
To get the unique element in a single-member set:
Use list ()
Class converts a collection object into a list.
Access the list at index 0.
my_set = {'jiyik.com'} my_list = list(my_set) element = my_list[0] print(element) # ????️ jiyik.com
Collection objects are not subscribeable (cannot be accessed via index), but lists are.
The first element in the list has index 0, so all we have to do is access the list element at index 0 to get the only member of the set.
Alternatively, we can use a for loop.
To get the unique element in a single member set:
Use a for loop to traverse the set.
Assign elements to variables.
my_set = {'jiyik.com'} element = None for element in my_set: break print(element) # ????️ jiyik.com
We use
for
to loop through the collection and assign its unique elements to variables. We also use thebreak
statement to exit thefor
loop.break
The statement breaks out of the innermost for or while loop.
There is no need to do this if our collection is guaranteed to contain only one element.
Alternatively, we can use the set.pop()
method.
Use the set.pop()
method to get the only element in a single-member collection, for exampleelement = my_set.pop()
. set.pop()
The method will remove and return the unique element from the set object.
my_set = {'jiyik.com'} element = my_set.pop() print(element) # ????️ jiyik.com print(my_set) # ????️ set()
set.pop
Method removes and returns an arbitrary element from the set.
If the collection is empty, this method will raise a
KeyError
exception.
If we need to handle KeyError
exceptions, please use the try/except
statement.
my_set = set() try: element = my_set.pop() print(element) print(my_set) except KeyError: # ????️ this runs print('The set object is empty')
set
The object is empty, so theset.pop()
method throws aKeyError
exception, which is then handled by the except block.
Use the set.pop()
method only when we need to retrieve and remove elements from the set.
The above is the detailed content of How to get the unique element in a single-member collection in Python. For more information, please follow other related articles on the PHP Chinese website!