Lists, tuples, and sets

Jose Salvatierra
A free video tutorial from Jose Salvatierra
Founder of Teclado and Software Engineer
4.6 instructor rating • 9 courses • 181,059 students

Lecture description

In this video we look at collections in Python: lists, tuples, and sets. These allow us to store multiple values in the same variable.

Learn more from the full course

Automated Software Testing with Python

Learn about automated software testing with Python, BDD, Selenium WebDriver, and Postman, focusing on web applications

13:24:44 of on-demand video • Updated November 2019

  • Everything you need to know about automated software testing with Python (and how to enjoy testing, too!)
  • Common pitfalls and best practices when writing automated tests
  • How to write complete system tests using Python and tools like Postman
  • Automate your application testing by setting up a continuous integration pipeline using Travis CI
  • Browser-based acceptance testing using Behave and Selenium WebDriver
English -: Hi, guys and welcome back. In this video, we're going to learn about lists, tuples, and sets. Three different collections in Python that allows us to store multiple values in a single variable. So that we can use them all at once or maybe extract some of them individually. Let's get started. We're going to define one of each just so you know how they are defined. So, l is going to be a list of three elements. And here I've created a variable called l for lists and I've made it equal to a list. The list is defined by using square brackets and inside the square brackets you can put different values as long as they are separated by a comma. So what I've got here is one string, then I've got a comma, then I've got another string, then I've got another comma, and then I've got another string. So three strings separated by commas inside square brackets. This defines a list. If you didn't have lists or tuples or sets, you would have to define three variables, one for each friend. And that would make it difficult to interact with all the values at once. You'll understand what I mean as we progress through the video. A tuple is very similar to a list but instead of using square brackets you use normal brackets. The key difference between lists and tuples is that you can't modify a tuple, whereas you can add and remove elements from a list, you can't add and remove elements from a tuple. That's the key difference between the two. Finally, a set is, again very similar, but you use curly braces instead of square or normal brackets. And the key difference between a set and the other two is that while you can add and remove elements from a set you can't have duplicate elements. So you couldn't have Bob twice in the set. Also, lists and tuples keep the order of the elements. So whenever you print out this list, it will always have Bob then Rolf and then Anne in that order, but sets don't necessarily keep the order, rather the order is not guaranteed. So the order could change at any moment. Sets are useful in a few scenarios that we will learn about in the coming videos. You can access individual elements of a list or a tuple by using subscript notation. So for example, you can print l. So what we've got here is our list variable and then we've got [0]. This notation here is called subscript notation and you can apply it to many things in Python but you can use it on lists and tuples and what it does is give you that element with index zero. Elements in programming usually start counting at zero, so the first element here, Bob has an index of zero. Then this one has an index of one, and this one has an index of two. So you could access Anne by doing l[2]. This also works for tuples so you can use your tuple variable there instead and that's totally find as well. However, it doesn't make sense to do that in sets because they don't have any order. So if you access the third element of a set, you don't know what you're going to get. That's why sets don't allow you to use subscript notation on them. You can modify individual items in a list by accessing the item itself using subscript notation and then you treating it like a variable. So for example, you can say l[0] equals Smith and then if you print your list and you run it, you will see that the first element is now Smith, second one's Rolf and the third one's Anne. However, if you're trying to modify a tuples element and you run this file, you will actually get an error because tuples cannot be modified after they're created so you can't use the equal sign on anything inside a tuple. Of course you can not do this on a set because a set does not allow for this subscript notation. You can add elements to a list by doing l.append and what append does is it will add an element to the end of the list. So if we append, for example, Smith to this list and then print it out you'll see that this list will now have four elements. That's Bob, Rolf, Anne, and Smith. But again because tuples to not allow modification, you can't change them, then if you do something like this you're also going to get an error and it's going to tell you that the tuple object has no attribute append. What that means is that we tried to access something called append inside, and that's what this full stop means so we're trying to access append inside of our tuple but our tuple doesn't have anything called append inside it. We're going to learn more about what I mean by inside in the next few videos. It is a little bit of a more complicated topic but rest assured you can't add things or remove things from a tuple. Talking about removal, you can remove things from a list. We're doing l.remove and then passing in the element that you want to remove. For example, if you wanted to remove Bob, then this would do that and then if you print(l) then you'll see that Bob has been removed and you are only left with Rolf and Anne. Going back to adding, you can add things to a set. So you can do s.add("Smith") then you can print your set and here you can see you that you get Anne, Bob, Smith, and Rolf. Notice that the order is not the same as when we created this, but all the elements are there. However, if you do the same thing twice, you'll see that Smith will only be here once. That's because, even though we told the set to add Smith twice there can be no duplicate elements inside a set, so this second Smith is actually ignored. This was a very brief primer or refresher regarding what lists, tuples and sets are. So just remember that lists are the most standard collection where you add and remove elements as well as modify them. In a tuple you have those elements and you can't modify them and you can't add or remove elements. And finally, a set, has no duplicate elements and it also has no order. That's everything for this video. Thank you for joining me. And I'll see you in the next one.