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”)
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?