Python lists, sets, and tuples explained 🍍

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello everybody today I will be explaining a few different types of collections in Python there's four general purpose collections three of them are lists sets and tuples there are also dictionaries but I'll save that for the next topic because they can be kind of tricky a collection I would think of them as a single variable and I'm saying that within quotes that is used to store multiple values that's how I would explain a collection to a beginner for example let's say we have a variable variable fruit fruit equals some value like Apple and then I can print this fruit which is Apple I could turn this variable into a collection by surrounding my values with either a set of square brackets for a list curly braces for a set or parentheses for a tuple let's begin with the list if I would like to store more than one value in this variable I will surround my values with a set of square brackets this variable is now a list I can store multiple values separated with the comma not only do we have an apple in this variable but we have an orange a banana and coconut one naming convention that I like to use if I declare a collection such as a list set or Tuple I like to take the variable name and make it plural just so that it's more obvious that this is a collection of values technically in the English language fruit would still be plural English is a weird language we now have a list of fruit named fruits if I were to print my list this is the result we have all of our values enclosed with a set of square brackets to access one of these elements found within your list you can use the index operator much like what we can do with strings the first element would have an index of zero that would print my value Apple index of one would be my orange two is banana three coconut what about four we don't have a value there list index out of range each value in a collection is also known as an element if we attempt to access an element that's not found within our collection you'll run into an index error with the index operator you could set a beginning index an ending index and a step I would like the first three elements you could say zero colon three that would give me Apple orange banana technically you don't even need the zero you need that colon though we can even use a step I would like every second element apple banana it's every second element beginning from index zero maybe I would like my fruit backwards I'll set the step to be negative one coconut banana orange Apple you can use the index operator with collections much like you can use with strings another cool thing you can do too with collections is that you can iterate over them with the for Loop four x in my collection fruits what would we like to do I will print whatever X is so we have iterated over our list Apple orange banana coconut now X isn't really too descriptive what you'll see some people do is that with their collection name it's plural their counter will be the singular version of that word if our collection name is fruits let's rename X as fruit singular it's not mandatory but that's a common convention it's more readable that way for every fruit in fruits If This Were cars you could say for car in Cars our counter is storing whatever value is within our collection so what are all the different methods that we can use with collections to list the different methods that are available to a collection you can use the dir function within this function add your collection fruits but we would need to print this let's surround this function with a print statement these are all in alphabetical order we have attributes which I have not explained yet but I will in a future topic but if we scroll to the end we have a bunch of different methods that this list can perform append clear copy account extend index insert pop remove reverse and sort if you would like a description of each of these methods there is a help function help add your collection to the parentheses then we would need to print this here's the description of all the methods and attributes for example we have our sort method and here's a description sort the list in ascending order and return none and then a bunch of other stuff if you ever forget what you're capable of with a list or other collection you can always use the help function to print a description of the attributes and methods available if you need the length of how many elements are within a collection there is the length function return the length of my list fruits then let's print it there's four elements within my list the length function returns four if I were to add an extra element like a pineapple then that number would be five let's remove that using the in operator we can find if a value is within a collection is our value Apple in fruits but then we would need to print this this operator will return a Boolean so let's print whatever that is is Apple in fruits that is true but is pineapple pineapple is not it's false you can use the in operator to find if a value is within a list and that applies for your other collections too with lists they're ordered and changeable duplicates are okay we can change one of these values after we create our list let's take fruits at index of zero I will set the sequel to be a pineapple then let's iterate over our fruit using a for Loop okay the first element is no longer an apple it's a pineapple then orange banana coconut using an index you can reassign one of the values if I were to change zero to one well now we have an apple pineapple banana coconut let's cover some of the methods that are found within a list we can append an element type the name of the list dot append what would we like to append to the end of this list let's append a pineapple I'm going to get rid of this for Loop I'm just going to display my list there we have an apple an orange banana coconut pineapple to add an element to the end of a list use the append method to remove an element you can use the remove method Roots dot remove let's remove our Apple our apple is no longer there we have an orange banana coconut using the insert method we can insert a value at a given index fruits dot insert list and index 0 would be the beginning then a value pineapple now we have a pineapple Apple orange banana coconut the sort method will sort a list fruits.sort these are all in alphabetical order now apple banana coconut orange to reverse a list you would use the reverse method fruits.reverse coconut banana orange Apple however these are not in Reverse alphabetical order these elements are reversed based on the order in which we place them if you would like reverse alphabetical order you can first sort and then reverse now we have orange coconut banana apple to clear a list use the clear method fruits.clear all of the elements are gone we can return the index of a value let's return the index of Apple fruits Dot index list and element then we will need to print this let's print the index that is returned the index of Apple is zero coconut that would be three what if we don't find a value like a pineapple well we have an error pineapple is not in list you could count the amount of times that a value is found within a list because duplicates are okay fruits dot count let's count how many bananas are in this list banana then print it one banana is found within this list how about pineapples there are zero now those are lists surround your values with a set of square brackets these values are ordered and changeable duplicates are okay now let's talk about the next collection which is a set to create a set surround your values instead with a set of curly braces our collection of fruits is now a set a set has different benefits the values are unordered and immutable meaning we can't alter these values however we can add and remove elements a set does not include any duplicates I'm going to delete these methods then print fruits we have all of the same values but they're not in the same order as they were originally a set is unordered if I were to run this again they will likely be in a different order see now we have a banana apple coconut orange to display all of the different attributes and methods of a set you can use the Der function and here's all of them some of these methods are a little more advanced but there's a few we might recognize like add clear copy for an in-depth description of these methods you can use the help function much like what we did before to find the length of our set we can use the length function which is for we can use the in operator to find if a value is found within the set unfortunately pineapples are not within our set now if I was to use the index operator of my set this is what would happen we have an error set object is not subscriptable we're not able to use indexing on a set because they're unordered much like what we can do with a list or a string we can't change the values of a set but we could add or remove elements let's use the add method to add guess what a pineapple that is okay orange Apple pineapple coconut banana we can remove an element fruits dot remove D let's remove our Apple our apple is gone coconut orange banana pop the pop method will remove whatever element is first but it's going to be random though orange coconut banana Apple coconut banana apple banana coconut you can clear fruits dot clear the elements of our set are gone those are a few of the more useful methods for beginners as a summary a set is a collection that is unordered and immutable you can't change the values but adding and removing elements is okay no duplicates are allowed let's try that real quick I'm going to add a second coconut yeah see we only still have one coconut sets may work well if you're working with Constance maybe colors for example you need to find if a color is within a set all right now lastly let's talk about tuples a tuple is a collection that is surrounded with a set of parentheses tuples are ordered and unchangeable duplicates are okay one benefit of a tuple over a list is that tuples are faster than lists if you're working with a collection and it's okay if the collection is ordered and unchangeable you might as well use a tuple because it's faster when I print our Tuple all of these values are surrounded with a set of parentheses again we have the dur function to display the attributes and methods there's not as many for a tuple for methods we only have count and index again there's also help to display a description of these attributes and methods you can find the length of a tuple with the length function we have five elements within here using the in operator we can find if a value is found within our Tuple our pineapple is not within our fruits so there's only two methods we have access to let's find the index of Apple fruits.index Apple then I will print whatever is returned apple is found at index 0. there is also count fruits dot count how many coconuts are found within our Tuple fruits count the Coconuts then print this how many coconuts we have two coconuts and then again with any of these collections they're iterable so you can iterate over them using a for Loop for fruit and fruits yep Apple orange banana coconut coconut all right everybody so those are collections think of them as a single variable used to store multiple values there's four general purpose collections for beginners lists sets tuples and then dictionaries which we'll talk about next each of them has unique benefits lists are ordered and changeable duplicates are okay a set is unordered and immutable but adding and removing elements is okay no duplicates allowed a tuple is ordered and unchangeable duplicates are okay and they are faster than lists use tuples if you can over a list but yeah those are a few collections in Python
Info
Channel: Bro Code
Views: 243,621
Rating: undefined out of 5
Keywords: Python tutorial for beginners, python course
Id: gOMW_n2-2Mw
Channel Id: undefined
Length: 15min 5sec (905 seconds)
Published: Fri Nov 04 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.