Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

Previous Tutorial Next Tutorial# Sets

Sets are lists with no duplicate entries. Let's say you want to collect a list of words used in a paragraph:

```
print(set("my name is Eric and Eric is my name".split()))
```

This will print out a list containing "my", "name", "is", "Eric", and finally "and". Since the rest of the sentence uses words which are already in the set, they are not inserted twice.

Sets are a powerful tool in Python since they have the ability to calculate differences and intersections between other sets. For example, say you have a list of participants in events A and B:

```
a = set(["Jake", "John", "Eric"])
print(a)
b = set(["John", "Jill"])
print(b)
```

To find out which members attended both events, you may use the "intersection" method:

```
a = set(["Jake", "John", "Eric"])
b = set(["John", "Jill"])
set(['John'])
a.intersection(b)
set(['John'])
b.intersection(a)
set(['John'])
```

To find out which members attended only one of the events, use the "symmetric_difference" method:

```
a = set(["Jake", "John", "Eric"])
b = set(["John", "Jill"])
a.symmetric_difference(b)
set(['Jill', 'Jake', 'Eric'])
b.symmetric_difference(a)
set(['Jill', 'Jake', 'Eric'])
```

To find out which members attended only one event and not the other, use the "difference" method:

```
a = set(["Jake", "John", "Eric"])
b = set(["John", "Jill"])
a.difference(b)
set(['Jake', 'Eric'])
b.difference(a)
set(['Jill'])
```

To receive a list of all participants, use the "union" method:

```
a = set(["Jake", "John", "Eric"])
b = set(["John", "Jill"])
a.union(b)
set(['Jill', 'Jake', 'John', 'Eric'])
```

In the exercise below, use the given lists to print out a set containing all the participants from event A which did not attend event B.

```
a = ["Jake", "John", "Eric"]
b = ["John", "Jill"]
```

```
a = ["Jake", "John", "Eric"]
b = ["John", "Jill"]
A = set(a)
B = set(b)
print(A.difference(B))
```

```
test_output_contains("['Jake', 'Eric']")
success_msg("Nice work!")
```

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join **over a million** other learners and get started learning Python for data science today!