Add items to set python

Python Set – How to Create Sets in Python

Ihechikara Vincent Abba

Ihechikara Vincent Abba

Python Set – How to Create Sets in Python

You can use sets in Python to store a collection of data in a single variable.

Each of the built-in data structures in Python like lists, dictionaries, and tuples have their distinguishing features.

Here are some of the features of sets in Python:

  • Duplicate items are not allowed. If items appear multiple times, only one will be recognized in the set.
  • The items in a set are unordered. The order of the set changes every time it is used.
  • The value of items in a set cannot be modified/changed once the set has been created.
Читайте также:  Уменьшить размер кнопки css

In this article, you’ll learn how to create sets. You’ll also learn how to access, add, and remove items in a set in Python.

We’ll conclude by talking about some of the use cases of sets in Python programming and mathematics.

How to Create Sets in Python

We use curly brackets to store the items in a set. Here’s what a set looks like:

In the code above, we created a set called nameSet .

You’ll notice that when the set was printed out, the values appeared in a different order. This is one of the features of sets in Python I mentioned above.

Here’s another example with one duplicated item:

The duplicate of «Jane» was ignored in the example above. This is because duplicate items are not allowed.

How to Access Items in a Set in Python

You can use a loop to access and print the items in a set. You cannot use the index of the items to access them since the order is always changing–- no item retains its index.

nameSet = for names in nameSet: print(names) # John # Doe # Jane

We use a for loop to print the items in nameSet

In the next section, you’ll see how to add items to a set.

How to Add Items to a Set in Python

You can add an item to a set in Python by using the add() method with the new item to be added passed in as a parameter.

nameSet = nameSet.add("Ihechikara") print(nameSet) #

We added a new item – «Ihechikara» – to the set: nameSet.add(«Ihechikara») .

You can also add an item from another set or other data structures (lists, dictionaries, and tuples) to a set by using the update() method.

nameSet = nameSet2 = nameSet.update(nameSet2) print(nameSet) #

In order to add the names from nameSet2 to nameSet , we passed in nameSet2 as a parameter to the update() method: nameSet.update(nameSet2) .

How to Remove Items in a Set in Python

There are different methods you can use to remove an item(s) in a set. Let’s look at them now.

How to Remove Items in a Set in Python Using the discard() Method

You can use the discard() method to remove a specified item. Here’s an example:

nameSet = nameSet.discard("John") print(nameSet) #

In the example above, «John» was passed as a parameter to the discard() method so it got removed from the set.

How to Remove Items in a Set in Python Using the remove() Method

The remove() method works the same way as the discard() method.

nameSet = nameSet.remove("Jane") print(nameSet) #

How to Empty a Set in Python Using the clear() Method

To clear all the items in a set, we use the clear() method.

nameSet = nameSet.clear() print(nameSet) # set()

When to Use Sets in Python

In this section, we’ll discuss two important use cases of sets in Python.

We can use sets to remove duplicate items in other data structures.

We can also perform some cool mathematical operations like getting the union, intersection, difference, and symmetric difference of two or more sets.

How to Use Sets to Remove Duplicate Items in Other Data Structures

We can use sets to get rid of duplicate items in other data structures like lists and tuples.

This is useful when you are dealing with a very large data set that requires only the individual unit of items returned instead of the number of occurrences of the items.

numberList = [2, 2, 4, 8, 9, 10, 8, 2, 5, 7, 3, 4, 7, 9] numberSet = set(numberList) print(numberSet) #

In the code above, we create a list of numbers called numberList which had multiple occurrences of some of the numbers: [2, 2, 4, 8, 9, 10, 8, 2, 5, 7, 3, 4, 7, 9] .

Using the set() method, we converted the list numberList to a set: numberSet = set(numberList) .

When the new set was printed out, all the duplicates of the numbers were removed – we only got one occurrence of each number returned: .

How to Perform Mathematical Operations Using Sets in Python

Sets in Python are similar to sets in mathematics, and we can get various results based on the relationship that exists between sets.

In this section, you’ll see how to get the union, intersection, difference and symmetric difference between sets in Python.

You can perform all the operations in this section using more than two sets. To keep it as simple as possible, we’ll make use of just two sets for each example.

How to Get the Union of Sets in Python

The union of two sets is the set of all the individual items that exist in both sets. In the union, duplicates are ignored.

firstSet = secondSet = print(firstSet | secondSet) #

In the example above, we have two sets – firstSet = and secondSet = .

Using the vertical bar operator ( | ), we were able to get the union of the two sets: firstSet | secondSet .

The union of the sets is as follows: . You can see that the two sets have been combined to form one set without any duplicates.

How to Get the Intersection of Sets in Python

The intersection of two sets is the set of items that are common to both sets. In our case, it is the set of items that appears in both firstSet and secondSet .

firstSet = secondSet = print(firstSet & secondSet) #

In this example, we’re using the & operator to get the intersection of firstSet and secondSet : firstSet & secondSet .

We got returned because 3 and 5 appear in both sets.

How to Get the Difference Between Sets in Python

The difference between two sets is the set of all the items that exist in one set but not the other.

firstSet = secondSet = print(firstSet - secondSet) #

In the example above, we are getting a set of the items that exist in firstSet but not in secondSet .

We used the — operator to achieve this: firstSet — secondSet .

The result from the operation is 2 and 4.

How to Get the Symmetric Difference of Sets in Python

The symmetric difference of two sets is the set of all items that exist in either of the two sets but not in both.

In the last section, we saw the result of items that exist in just one set but not the other. The symmetric difference is the result of items that exist in each set but not in both.

firstSet = secondSet = print(firstSet ^ secondSet) #

We used the ^ operator to get the symmetric difference of two sets: firstSet ^ secondSet .

The result was 1, 2, 4, 7. Each of these items do not appear in both sets.

Summary

In this article, we talked about sets and how to create them in Python.

Sets do not allow duplicate items, they are unordered, and the items stored in them cannot be modified.

We also saw how to access, add, and remove items in sets using different methods.

Lastly, we talked about when to use sets in Python. We saw some of the applications of sets in Python and its use in mathematical operations.

Источник

Python — Add Set Items

Once a set is created, you cannot change its items, but you can add new items.

To add one item to a set use the add() method.

Example

Add an item to a set, using the add() method:

Add Sets

To add items from another set into the current set, use the update() method.

Example

Add elements from tropical into thisset :

Add Any Iterable

The object in the update() method does not have to be a set, it can be any iterable object (tuples, lists, dictionaries etc.).

Example

Add elements of a list to at set:

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Оцените статью