Loading video player…

Covering Collection Types

Resources mentioned in this lesson:

00:00 In the previous lesson, I covered the built-in functions that create and manage byte data. This is the last of the lessons on data type functions, where I’ll be covering collection types.

00:11 Keeping with its batteries-included mentality, Python comes with a bunch of different ways of handling groups of data. And all of the built-in collections have corresponding built-in functions, including lists, tuples, dictionaries, sets, and frozen sets.

00:28 Any guesses as to where I’m going to go next? Give me an R. Lists are probably one of the earlier data types you come across when learning Python and are in close running with dictionaries for the ones I most frequently use.

00:41 The list() function is a constructor similar to the ones you’ve seen before. What is a little different here is that the list() function treats its argument as an iterable and creates a new list with each item from the iteration as an item in the list.

00:58 So when you pass in a list, you get back an equivalent list. And since tuples are iterable, if you pass in a tuple, it converts it to a list. Beyond this, it can get a little counterintuitive, though. Strings are iterable.

01:15 When you iterate over a string, you get back each character in that string, which means when you pass a string to the list() function, you get back a list of the characters in the string.

01:34 Following that logic, what do you think will happen when I pass this dictionary to the list() function?

01:41 Again, a little counterintuitive. This happens because when you iterate over a dictionary, you get the keys in the dictionary. If you want the contents of a dict, you can use the .values() method on the dict which returns an iterable of the contents.

01:58 The .items() method

02:03 returns a series of tuples of key-value pairs. None of this is really specific to what the list() function is doing. Any iterable you pass in will be iterated over.

02:14 Everything I just said about list can be re-said about tuples.

02:20 Iterating over a list,

02:22 a string,

02:25 or a dict, and you get the iterated items as members of the tuple. You just saw me create a dictionary using the brace bracket notation, but there’s another way as well, the dict() function.

02:38 This one is a little different in that it uses keyword arguments. The names of your keywords become the keys in the dict, while their content becomes the values.

02:56 Here, the name, age, and city keyword arguments become key-value pairs in the dictionary called jane.

03:04 Jane, the man they call Jane. Little random Firefly fan Easter egg. Using the dict() function is about the same amount of typing as just using brace brackets, but it can be useful because the dict() function takes more than just keyword arguments.

03:21 If you supply a dictionary as a positional argument, you can build upon it.

03:31 So if you ever need to copy a dict, this is one way to do it, and you can add new key-value pairs to the result at the same time. The dict() function can also take an iterable of tuples, where each tuple is a key-value pair.

03:53 This gives you a lot of flexibility. Your data can come in a variety of forms, and you can still quickly get a dictionary out of it by calling the dict() function.

04:02 A set is an unordered collection of unique items. There are two kinds of sets in Python, the regular one, which is mutable, and a frozen one, which is immutable.

04:13 Like with the list() function, the constructor functions for sets takes an iterable.

04:20 The set() function iterates over its argument and builds a set from the items. As sets only contain unique values, the two copies of the numbers 1 and 2 from my list get reduced to a single instance each. Like with list, you also get that possibly unexpected behavior with strings.

04:38 Iterate over a string, and you get the characters inside. Two things to note, though. First, you only get one L, because you’re only allowed one of each in a set. Second, sets are unordered, so they can come back in any jumble.

04:57 Frozen sets behave the same way, but return a frozen set object instead. This is an immutable version of the set. I don’t find I use these very frequently, but one possible situation is that frozen sets are hashable, which means they can be used as keys in a dictionary.

05:16 Sets, because they’re mutable, can’t be used as keys in the dictionary. So if you’re in that situation where you want to use a set as a key, converting it to a frozenset() will let you do that.

05:28 If you need a refresher on any of the core Python data types, there are video courses and tutorials on lists, tuples, dictionaries, and sets for your reading and watching pleasure.

05:40 That was the last of the data type creating built-in functions. Next up, some functions for iterators and iterables.

Become a Member to join the conversation.