In Python set is a collection type of basic data type. It has two types: mutable collection (set()) and immutable collection (frozenset). The operations of creating a set, adding a set, deleting a set, intersection, union, and difference are all very practical methods.
Create a collection set (recommended learning: Python video tutorial)
The python set class is in the sets module of python. Everyone In the python2.7.x currently used, sets can be created directly without importing the sets module.
>>>set('boy') set(['y', 'b', 'o'])
Collection addition
There are two common methods for adding python collections, namely add and update.
Collection add method: is to add the element to be passed in to the collection as a whole, for example:
>>> a = set('boy') >>> a.add('python') >>> a set(['y', 'python', 'b', 'o'])
Collection update method: is to split the element to be passed in, and do Pass the individual into the collection, for example:
>>> a = set('boy') >>> a.update('python') >>> a set(['b', 'h', 'o', 'n', 'p', 't', 'y'])
For more Python-related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of How to append elements to a collection in python. For more information, please follow other related articles on the PHP Chinese website!