SoFunction
Updated on 2024-10-30

Python operations for adding values to a collection

Add only one value to a collection with the () function

Mathematically, a collection is a logically related set of distinct objects. In Python, a collection is a built-in data type that is index-free and immutable.

This means that we can access the collection items through some specific indexes and we cannot modify the existing data within the collection.

We can declare a set by creating an object of the set class in Python. We can use the built-in add() method to append values to our newly created set.

The following code snippet demonstrates how we can append values to a collection using the add() method in Python.

myset = set()
(14)
(14)
(15)
print(myset)

Output:

{14, 15}

The output above demonstrates another property of collections not mentioned earlier. A collection contains only distinct values, so we cannot have duplicate values within a collection.

The only drawback of the add() method is that it can only add one value to our collection.

() function adds multiple values to a collection

The add() method works fine, but it takes only one input and if we want to add thousands of values to a collection, then our task becomes very tedious. In this case, we can utilize the built-in update() method, which can add multiple values to our collection at once.

The update() method takes an iterable object as input, iterates over it, and adds each item to our collection. Our iterable object does not need to be a list at any time.

The following code snippet shows us how to add multiple values to our collection using Python's update() method.

([1110,3,4])
print(myset)

Output:

{1, 3, 4, 14, 15, 1110}

We use the update() method in Python to add new values to the collection in our previous example.

While it's a clear winner in terms of saving time, the update() method has its drawbacks. For example, if we want to add a string to our collection, the update() function will iterate through the entire string and add each unique character to our collection, as shown in the following code example.

("this is my value")
print(myset)

Output:

{1, 3, 4, 's', 'm', 'a', 'h', 14, 15, 'l', 'y', 'u', 'e', 1110, 'v', 't', ' ', 'i'}

This is because a string is an iterable object in Python. We have to wrap our string around another iterable object (like a list or a set) to solve this problem.

This step is shown in the code example below.

(["this is my value"])
print(myset)

Output:

{1, 3, 4, 'm', 14, 15, 'i', 's', 'l', 'y', ' ', 'u', 'h', 1110, 'v', 'this is my value', 't', 'a', 'e'}

This time, the entire string is added to our collection instead of each character.

To this point this article on Python to add a value to a collection of methods of operation of the article is introduced to this, more related to Python to add a value to a collection of content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!