Detailed explanation of Python collection add() function, adding elements to the collection

"Author's Homepage": Shibie Sanri wyx
"Author's Profile": CSDN top100, Alibaba Cloud Blog Expert, Huawei Cloud Share Expert, Network Security High-quality Creator
"Recommended Column": Xiaobai Zero Basic "Python Beginner to Master"

The add() function can "add" elements to a collection.

grammar

set.add( element )

parameter

  • element : (required) the element to be added

return value

  • None, no return value, the value modifies the original collection.

Example: Adding an element to a collection

set1 = {
    
    1, 2, 3}
set1.add(4)
print(set1)

output:

{
    
    1, 2, 3, 4}

1. The order of elements

add() does not insert an element at the "end" of the collection . Because collections "do not guarantee" the "order" of elements , each time we print, the position of the elements will change.

set1 = {
    
    1, 2, 3}
set1.add('ZhangSan')
set1.add('LiSi')
set1.add('WangWu')
set1.add('QianLiu')
set1.add('SunQi')

print(set1)

output:

{
    
    1, 2, 3, 'ZhangSan', 'QianLiu', 'WangWu', 'LiSi', 'SunQi'}

As can be seen from the output results, the elements are not displayed in the order they were added; try printing multiple times, and you will find that the order is different each time.

It should be noted that the order of elements will not change when printing in a "circular" manner

set1 = {
    
    1, 2, 3}
set1.add('ZhangSan')
set1.add('LiSi')
set1.add('WangWu')
set1.add('QianLiu')
set1.add('SunQi')

i = 0
while i < 10:
    print(set1)
    i += 1

output:

{
    
    'WangWu', 1, 2, 3, 'SunQi', 'ZhangSan', 'LiSi', 'QianLiu'}
{
    
    'WangWu', 1, 2, 3, 'SunQi', 'ZhangSan', 'LiSi', 'QianLiu'}
{
    
    'WangWu', 1, 2, 3, 'SunQi', 'ZhangSan', 'LiSi', 'QianLiu'}
{
    
    'WangWu', 1, 2, 3, 'SunQi', 'ZhangSan', 'LiSi', 'QianLiu'}
{
    
    'WangWu', 1, 2, 3, 'SunQi', 'ZhangSan', 'LiSi', 'QianLiu'}
{
    
    'WangWu', 1, 2, 3, 'SunQi', 'ZhangSan', 'LiSi', 'QianLiu'}
{
    
    'WangWu', 1, 2, 3, 'SunQi', 'ZhangSan', 'LiSi', 'QianLiu'}
{
    
    'WangWu', 1, 2, 3, 'SunQi', 'ZhangSan', 'LiSi', 'QianLiu'}
{
    
    'WangWu', 1, 2, 3, 'SunQi', 'ZhangSan', 'LiSi', 'QianLiu'}
{
    
    'WangWu', 1, 2, 3, 'SunQi', 'ZhangSan', 'LiSi', 'QianLiu'}

2. Types of elements that can be added

The elements added by add() can be "string" , "tuple" , or "bytes type" , but not list, set, or dictionary types.

set1 = {
    
    1, 2, 3}

set1.add('str')
set1.add((4, 5, 6))
set1.add(b'123')
print(set1)

output:

{
    
    1, 2, 3, (4, 5, 6), 'str', b'123'}

When adding an element type that is not allowed, an error will be reported TypeError: unhashable type: 'list'

insert image description here


3. Add repeated elements

The elements in the collection are not allowed to be "repeated" . If you add an element that "already exists" in the collection , the added operation will not be performed, and of course, no error will be reported.

set1 = {
    
    1, 2, 3}
set1.add(3)
print(set1)

output:

{
    
    1, 2, 3}

4. Only one element can be added at a time

add() only accepts one parameter, which means that only "one element" can be added at a time , otherwise an error will be reported TypeError: add() takes exactly one argument

set1.add(1, 2)

insert image description here


Guess you like

Origin blog.csdn.net/wangyuxiang946/article/details/131794219