Loading...

Sets in Python

Python Sets

In Python, sets are a powerful data structure used to store multiple items in a single variable. This lesson will explore the characteristics of sets, how to create them, and their usage in Python, emphasizing important SEO keywords such as "Python sets," "set items," and "set data types."

1. What is a Set?

A set is one of the four built-in data types in Python used to store collections of data. The other three are list, tuple, and dictionary, each with its unique qualities and usage.

Characteristics of Sets:

  • Unordered: The items in a set do not have a defined order.
  • Unchangeable: Once a set is created, you cannot change its items, although you can add or remove items.
  • Unindexed: Set items cannot be accessed by index or key.
  • No Duplicate Values: Sets do not allow duplicate values.

Example: Create a Set

python
3 lines
|
34/ 500 tokens
1
2
3
# Creating a set
fruits_set = {"apple", "banana", "cherry"}
print(fruits_set)  # Output: {'banana', 'cherry', 'apple'} (Order may vary)
Code Tools

Note: Since sets are unordered, the items may appear in a different order each time you use them.

2. Set Items

Set items possess specific characteristics:

2.1 Unordered

Items in a set do not have a defined order. This means that the items can appear in different orders and cannot be referred to by index.

2.2 Unchangeable

While you cannot modify the items in a set directly, you can remove existing items and add new ones.

Example: Attempt to Add Duplicate Values

python
3 lines
|
47/ 500 tokens
1
2
3
# Demonstrating that duplicate values are ignored
fruits_set = {"apple", "banana", "cherry", "apple"}
print(fruits_set)  # Output: {'banana', 'cherry', 'apple'} (Duplicates are ignored)
Code Tools

2.3 Duplicate Values

Sets do not allow duplicate values. For example, the values True and 1 are considered the same value in sets.

Example: True and 1 Considered the Same

python
3 lines
|
49/ 500 tokens
1
2
3
# Example showing True and 1 are treated as duplicates
mixed_set = {"apple", "banana", "cherry", True, 1}
print(mixed_set)  # Output: {'banana', 'cherry', 'apple'} (True and 1 are counted as one)
Code Tools

Example: False and 0 Considered the Same

python
3 lines
|
51/ 500 tokens
1
2
3
# Example showing False and 0 are treated as duplicates
another_set = {"apple", "banana", "cherry", False, 0}
print(another_set)  # Output: {'banana', 'cherry', 'apple'} (False and 0 are counted as one)
Code Tools

3. Get the Length of a Set

To determine how many items a set contains, you can use the len() function.

Example: Get the Number of Items in a Set

python
3 lines
|
24/ 500 tokens
1
2
3
# Original set
fruits_set = {"apple", "banana", "cherry"}
print(len(fruits_set))  # Output: 3
Code Tools

4. Set Items - Data Types

Set items can consist of any data type. A set can include strings, integers, and booleans.

Example: Different Data Types in Sets

python
8 lines
|
75/ 500 tokens
1
2
3
4
5
6
7
8
# Sets with various data types
set_of_strings = {"apple", "banana", "cherry"}
set_of_numbers = {1, 5, 7, 9, 3}
set_of_booleans = {True, False}

# A set containing different data types
mixed_set = {"abc", 34, True, 40, "male"}
print(mixed_set)  # Output: {True, 40, 'abc', 'male', 34} (Order may vary)
Code Tools

5. What is the Data Type of a Set?

From Python's perspective, sets are defined as objects with the data type 'set'.

Example: Check the Data Type of a Set

python
3 lines
|
29/ 500 tokens
1
2
3
# Check the data type of a set
my_set = {"apple", "banana", "cherry"}
print(type(my_set))  # Output: <class 'set'>
Code Tools

6. The set() Constructor

You can also create a set using the set() constructor, which allows for the creation of a set from other iterable collections.

Example: Using the set() Constructor

python
3 lines
|
51/ 500 tokens
1
2
3
# Creating a set using the set() constructor
fruits_set = set(("apple", "banana", "cherry"))  # Note the double round brackets
print(fruits_set)  # Output: {'banana', 'cherry', 'apple'} (Order may vary)
Code Tools

Frequently Asked Questions

A set in Python is an unordered collection of unique items. It is a built-in data type that allows for fast membership testing and eliminates duplicates automatically.

The key difference is that sets are unordered and do not allow duplicates, whereas lists are ordered and allow duplicates. Sets are also more efficient for membership tests.

Yes, you can use the set() function with strings. It will treat each character as an individual element and store them in an unordered collection with no duplicates.

Use sets when you need to ensure uniqueness and perform operations like union, intersection, or difference. Use lists when order matters or when you need to store duplicate values.

You can add elements to a set using the add() method. This method adds a single element, ensuring that it doesn't already exist in the set.

Still have questions?Contact our support team