How to append elements to a collection in python

(*-*)浩
Release: 2019-07-09 10:23:53
Original
30415 people have browsed it

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'])
Copy after login

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'])
Copy after login

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'])
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!