<jupyter_start><jupyter_text>5 Lists, Tuples and SetsIn [Chapter 5: Lists and Tuples](../5_Lists_Tuples.ipynb) we had a look at the properties of *lists* and *tuples*. *Sets* are another valuable Python collections, however they're used less on a day-to-day base. For the sake of completeness, we will cover them here in some extra course materials.We also included a section on how to extract values from a list or a tuple by means of destructuring or unpacking. This method is sometimes a better alternative to slicing.  5.1 Sets  *Sets* are also a collection of multiple values, but they are _unordered_ and all values in a set are _unique_. It is much easier to compare them to each other. Because sets cannot have multiple occurrences of the same element, it makes sets highly useful to efficiently remove duplicate values from a list or tuple and to perform common math operations like unions and intersections.![sets](../images/Python-Set-Operatioons.png)Source: https://www.learnbyexample.org/python-set/You initialise them by using **set()** on a list or tuple:<jupyter_code>myList = [1 , 3, 5, 6, 6]
mySet = set(myList)
mySet<jupyter_output><empty_output><jupyter_text>**Question**: How would you create an empty `set()`?<jupyter_code># Create an empty set
newSet = set()
newSet
dir(mySet)
mySet1 = set(range(10))
mySet2 = set(range(5,20))
 
print(f"{mySet1}")
print(f"{mySet2}")
 
mySet2.add(5)  # Elements in a set are unique - the set will not change because it already has a 5
print(mySet2)

print(mySet1.intersection(mySet2))
print(mySet1.union(mySet2))<jupyter_output>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
{5, 6, 7, 8, 9}
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}<jupyter_text>The principle of using intersection and union is the same as the Venn diagrams you probably saw in school... You can also make a set out of a string:<jupyter_code>myString = "This is a sentence."

myLetters = set(myString)
myLetters    # Note that an upper case T and lower case t are not the same!<jupyter_output><empty_output><jupyter_text>There are more things you can do with sets which we will not go into here, see the [Python sets](https://docs.python.org/3/library/stdtypes.htmltypes-set) documentation for more information. --- 5.5.1 ExerciseWhich letters are shared between the words "perspicacious" and "circumlocution"?---  5.2 De-structuring or UnpackingThere are times when you will want "pull values out of" a list, tuple, or set and give individual values a name.You can of course achieve this like so:```pythonmyList = ['a', 'b', 'c', 'd']first = myList[0]second = myList[1]rest = myList[2:]```This is perfectly fine in many situations. But, as we will see later, it doesn't work in situation where you cannotperform assignment. But fear not! Python to the rescue! You can de-structure collection structures to name individualelements. Using this technique, the above example becomes:<jupyter_code>myList = ['a', 'b', 'c', 'd']
first, second, *rest = myList
print(f"{first} {second} {rest}")
myString = "ab"
fst, snd = myString
(fst, snd)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11114570361145704, 'fraction_numerical': 0.04078455790784558, 'mean_word_length': 4.841818181818182, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '475888', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1149, 'n_tokens_neox': 1027, 'n_words': 456, 'path': 'extra-course-parts/05_Lists_Tuples_Sets_Extra.ipynb', 'repo_name': 'vibbits/gentle-hands-on-python', 'size': '3212'}