# Introduction to Python zero Basics - 16 - collections in Python

## What is a collection?

• A set is an unordered and unrepeatable sequence
• Its basic usage includes member detection and de duplication
• Set objects also support mathematical operations such as union, intersection, difference set, symmetric difference and so on
• All elements in the collection are placed in the middle of {} and separated by commas

### definition

A set is an unordered and non repetitive sequence. All elements in the set are placed in the middle of {} and separated by commas, for example:

• {1, 2, 3}, a list of 3 integers
• {'a', 'b', 'c'}, a list of three strings

Examples of collections

```# statement
cars = {'audi', 'bmw', 'Benz', 'bmw', 'Benz', 'Wuling'}
print(cars)

set_ = {1, 1, 3, 1, 2}
print(set_)

# Output results
{'orange', 'pear', 'banana', 'apple'}
{1, 2}
```

Automatic de duplication. Only one element is reserved for each element, and it is out of order

Let's take another example

```set_ = {{1, }, {1, }}
print(set_)

set_ = {[1, ], [2, ]}
print(set_)

# 	Output results
#   set_ = {{1, }, {1, }}
#	TypeError: unhashable type: 'set'

#   set_ = {[1, ], [2, ]}
#	TypeError: unhashable type: 'list'
```

Why report an error?

**Important: * * because the set set can only contain immutable object elements, and the list and set itself are variable objects, an error will be reported

### The difference between set and list

In Python, the difference between a set and a list is as follows:

• The elements in the list are allowed to be repeated, and the elements in the collection are not allowed to be repeated. Examples are as follows:
```>>> x = {1, 1, 2, 3}
>>> x
{1, 2, 3}
```

In the above procedure, element 1 is repeated twice. When creating the set, the repeated elements are combined into one.

• The list is ordered and provides index operation. The collection is unordered and there is no index operation. An example is as follows
```>>> x = {1, 2, 3}
>>> x[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing
```

In the above program, you tried to use the index operator to access the element of item 0 of the collection. The collection is out of order and does not provide index operation. An error occurs when running. The prompt: 'set' object does not support indexing.

### Special set

How to create an empty collection

```set_ = {}
print(set_, type(set_))

# Output results
{} <class 'dict'>
```

You cannot directly {} because it defaults to an empty dictionary

The correct way to create an empty set

```set_ = set()
print(set_, type(set_))

# Output results
# set() <class 'set'>
```

### Common operation

#### Operator|

Merge multiple sets

```>>> {1, 2} | {3, 4}
{1, 2, 3, 4}
>>> {1, 2} | {3, 4} | {5, 6}
{1, 2, 3, 4, 5, 6}
```

#### Operator-

Remove element from collection

```>>> {1, 2, 3, 4} - {3, 4, 5, 6}
{1, 2}
```

#### Operator&

Take the intersection and return the elements contained in both sets

```>>> {1, 2, 3} & {3, 4, 5}
{3}
```

#### Operator^

Take difference set

```>>> {1, 2, 3} ^ {3, 4, 5}
{1, 2, 4, 5}
```

#### Keyword in

Checks whether the collection contains the specified element

```>>> 'jack' in {'lily', 'jack', 'hanmeimei'}
True
>>> 'neo' in {'lily', 'jack', 'hanmeimei'}
False
```

## Common methods

The add(item) method adds an element item to the collection. The example is as follows:

```>>> x = {1, 2, 3}
>>> x
{1, 2, 3, 4}
```
• In line 2, use the add method to add an element 4 to the collection
• In line 4, an element has been added to the display list

### remove(item) method

The remove(item) method deletes the specified element item from the collection. An example is as follows:

```>>> x = {'neo', 'lily', 'hanmeimei'}
>>> x.remove('hanmeimei')
>>> x
{'neo', 'lily'}
```
• In line 2, use the remove method to remove an element 'imooc' from the collection
• In line 4, an element has been deleted from the display list

### clear() method

The clear() method removes all elements in the collection. An example is as follows:

```>>> x = {1, 2, 3}
>>> x
{1, 2, 3}
>>> x.clear()
>>> x
set()
```
• In line 1, a collection of three elements is created
• In line 4, use the clear() method to remove all elements from the collection
• In line 5, the set is displayed, and the result shows that all elements have been deleted

### union() method

The union() method returns the union of two sets, as shown in the following example:

```>>> x = {1, 2, 3}
>>> y = {4, 5, 6}
>>> z = x.union(y)
>>> z
{1, 2, 3, 4, 5, 6}
```
• In line 3, use the union() method to return the union of sets x and y
• In line 4, the union of the two sets is displayed

### intersection() method

The intersection() method returns the intersection of two sets. An example is as follows:

```>>> x = {1, 2, 3}
>>> y = {2, 3, 4}
>>> z = x.intersection(y)
>>> z
{2, 3}
```
• In line 3, use the intersection() method to return the intersection of set x and set y
• In line 4, the intersection of the two sets is displayed

### issubset() method

The issubset() method determines whether the specified set is a subset. An example is as follows:

```>>> x = {1, 2, 3}
>>> y = {1, 2}
>>> y.isubset(x)
True
```
• In line 3, use the issubset() method to determine whether set y is a subset of set x
• In line 4, the result shows that set {1,2} is a subset of set {1,2,3}

### issuperset() method

The issuperset() method determines whether the specified set is a superset. An example is as follows:

```>>> x = {1, 2, 3}
>>> y = {1, 2}
>>> x.isuperset(y)
True
```
• In line 3, use the issuperset() method to determine whether set x is a superset of set y
• In line 4, the result shows that set {1,2,3} is a superset of set {1,2}

## Section

The special feature that there are no duplicate elements in the collection is used to do a lot of things. For example, a cashier system needs to input all the sales information into the system. In order to avoid repeated input of the information of a cashier, you only need to store the information in the collection to avoid this error.

Keywords: Python Back-end

Added by cmpennington on Fri, 25 Feb 2022 02:10:58 +0200