What is a Python set? What is a list in Python, for that matter? This article will give an overview of these two data structures and show how to add list values to a set in Python.

To explain the differences between sets and lists in Python – and to help you understand how to add a list to a set correctly – let’s start with an example of these data structures. Look at the two lines of code below:

pets = [“dog”, “cat”, “red fish”]
pets = {“dog”, “cat”, “red fish”}
pets = (“dog”, “cat”, “red fish”)

Have you ever seen these Python data structures? The first one is a List, the second one is a Set, and the last one is a Tuple. All are used to store collections of data. But how is a set different from a list or a tuple?

About Author

Alexandre Bruffa

I write cool stuff about Unity3D and AWS. I’m also an AWS Community Builder, and I’m AWS certified.