Home >Backend Development >Python Tutorial >How to append elements to a collection in python

How to append elements to a collection in python

(*-*)浩
(*-*)浩Original
2019-06-29 09:18:5930534browse

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.

How to append elements to a collection in python

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:python data analysisNext article:python data analysis