Python Lists | a complete tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video we shall understand Python list and how to work with them list is one of the popular data structures in Python it is basically a collection of certain items the items are separated by comma and the list is enclosed in between square brackets example let's declare a list of strings list of strings equals and in between the square brackets we shall write cat dog cow separated by commas also remember that strings must be enclosed in between single or double quotes let's string this list and this is of the output looks next let's declare a list of numbers list of numbers equal to 2 4 8 9 10 and 22 let's bring this we can also I have a list of lists in this example we have 4 lists inside the main list the fourth list again has two lists in silent let's print our list and check its value so we can we can see that the list has 4 less inside it and yes lists can also have mixed items like string integer float list etc which we can see in this example list effects and items equal to hello 23 4.0 which is a floating number and then we have a list hi and 12 and then we have a string ABC we shall see some of the methods to create lists empty list an empty list can be created by just specifying open square brackets followed by a closed one let's create a string list of strings list of strings equal to Python is an awesome let's print this let's see a list of numbers list of numbers equal to 1 2 5 and 35 now let's create a list of numbers using list comprehension or using the for loop list of num equal to I for I in range 0 comma 10 here range 0 comma 10 gives numbers from 0 to 10 the statement I for I in basically tells to select each item I in the range 0 comma 10 there is an easier way to do this list of num equal to list of range of 10 basically we are converting the range values to lists let's look at another example of creating even numbers less than 10 using list comprehension in this case we will just add a step to in in the range which skips the odd numbers since we mentioned 0 as the starting number so you can observe our list has values 0 2 4 6 & 8 and yes the same thing can also be achieved with below code list of num - equal to list of range of 0 comma 10 comma 2 let's print this so yeah we have the same results creating lists from comma-separated strings let's create a string with comma separated works words to add each word in this string to a list we can use dot split method and pass comma as the delimiter let's print the list so we can see that each word is added into a list next we will see how to put all characters in a string to list let's declare a string str2 equal to hello so to put each letter in this word to a list we need to simply write lists of str to let's print this print s they are to list so we can see that each letter in the string is made as a list item let's try the same thing with India's the thing which we tried with strings will not work here the trick is to convert the integer to string temporarily and then convert each string digit back to int and putting it into the list next we will convert the dictionary key or value to list let's declare a dictionary with key and value pairs ABCD are the keys and 1 2 3 4 are their respective values let's print the dictionary first let's add all the keys in the dictionary to a list to do this we simply have to write key least equal to list of my deck bought keys so we can observe that the keys ABCD are added to the list the list similarly to add all the values to a list we simply have to write value list equals list of my dick dot values so this will basically add all the values of our dictionary 1 2 3 4 to the list let's now look at the popular built-in functions for lists in Python lists that append method is used to append a single element at the end of the list let's declare a list with three items cat cow cow and dog let us append an item to our list using the append method list 1 dot append goat so we need to pass the list height of the items in between the round brackets let's print it now let's add another item which is number list 1 dot append 125 print list 1 so we can observe that the number 125 is appended at the end let's try to append a list of two items pirate and penguin and see what happens so you can observe that two items were added as a single element as a list so if you wanted all the list items to be appended separately the next function which we are going to discuss will help you with that list dot X new method is used to append each element of one list to another list let's create a list list to equal to penguin parrot and crane let's create another list list three equal to sparrow and goose we shall use list of extent method to append each element in list three the list to let's print list two and we can observe that each element of this three is added as a separate element or a separate item in list two next let's look at list dot insert method which is used to insert an item at a particular location let's declare a list list or equal to penguin parrot crane Sparrow and goose let's say we want to insert our in the third position or second index since index in Python starts with zero we can use less dot insert method which takes two arguments first one is the index where you want to insert and the second one is the value of the item which you want to insert let's say we want to insert duck after Sparrow in order to do this we need to get the index of Sparrow list dot index why that can be used to fetch the index of an item in the list so let's use it spero index equal to list to dot index of sparrow we can see that index of sparrow is four so in order to insert duck after sparrow we need to insert it in the fifth index which is nothing but one index more than sparrow so let's write list two dot insert Sparrow index plus one comma duck next we shall see list dot remove method which removes an element from the list let's create a list list two equal to penguin parrot owl and crane let's use list dot remove method and remove direct from the list so we can see that parrot has been removed from our list the list dot dot method removes and returns us the element from the list let's create a list list to equal to penguin parrot owl and crane the list dot method without any argument passed will return as the last item from the list so if we print the popped item we can see that it's the last element in the list which is crane we can also pass the index of an ID of an element to the list not pop method so that it returns the item of the given index so in this case we are trying to remove and get the element at index 1 which is parrot the list dot sort method can be used to sort to sort the list either in ascending or descending order let's create a list list 3 equal to 6 1 3 8 and 5 let's try to sort this list in ascending order using list dot sort method so by default if we do not pass any argument the method will sort the list in ascending order so if we want to sort the list in descending order we have to pass an argument reverse equal to to the list dot reverse method is used to reverse the list items let's create a list place 3 equal to 6 1 3 8 & 5 and reverse this list using list dot reverse method so 5 8 3 1 6 is the reverse list please note that it is simply reversing the list items not sorting them in descending order next we will see a lien method to get the length or number of items in a list let's create a list list 3 equal to penguin Owl Sparrow and duck we can find the length of the list by writing length equal to le an equal a lien of list 3 so we can see that our list has 4 items next let's see min and Max functions to find the minimum or maximum value in a list let's create a list list 3 equal to 5 8 3 1 & 6 to find the largest number in this list we can write max value equal to max of list 3 so we can see that the maximum value is 8 in our list so similarly to find the minimum value min value equal to men of history and the minimum value is 1 next we shall see list or count method to find how many times an item appears in the list so let's create list this 4 equal to a comma a comma B comma C comma a let's say we want to find the number of times the letter a appears in the list so we can find this with count a equal to list for the count of a so we can see that a appears three times in the list list the clear method to remove all the elements in the list so let's declare the list list for equal to K comma a comma B comma C comma a and now if we write a list for clear and check all the elements will be removed and an empty list remains so to check if an item exists in the list so if we need to check if an item exists in the list one of the way is to use if statement let's create a list this Phi equal to cat dog and spirit so if we need to check if dog exists in our list we have to write if dog in list PI and if the condition satisfies we will print list Phi hash dog so we can observe that list Phi has dog in it next we shall look at different ways of indexing the Python list so let's first create a list the indexing of list starts from zero that means if you want first element you have to pass index as zero for 5th element you need to pass index as 4 so let's try to fetch the first element by passing the index as 0 the index needs to be passed in the square braces so 2 is our first item to get the last item unit you can pass minus 1 in the index so 20 is the last item we can also get last item with this code last item equal to list 6 of Elian of list 6 minus 1 so basically Elian of our length of list 6 gives the length of our list since indexing starts from 0 to get the last item we need to subtract 1 from the length of our list so to get the last second item we can write lists 6 of minus 2 next let's look at slicing examples slicing basically helps in fetching specific portion of our list this can be achieved by passing the starting and end index let's create our list 2 4 6 8 10 12 14 16 18 9 20 let's say we want to get the first three elements from the list to fetch this we can write list six of zero is degree basically zero is two three will fetch the items with index zero one and two which are nothing but our values two four and six please note that it does not fetch items with index three even though we have specified three in the square braces next we will look at slicing with increment or skipping we can basically pass one more number after end index which basically skips the next element in this example list six of zero is two five is two two we are fetching elements from index 0 to index four but skipping one element in between so if we pass to one element will be skipped if you pass three two elements will escape in this example we can see that after two for a script six is fetched and a descriptor let's try passing 3's step number so we can observe upper two four and six is skipped slicing in reverse order so in order to slice the list in reverse order we need to write minus 1 as the step number so list six of is to is to minus one you can see that we are trying to slice all the elements in reverse order in the next example we will slice the elements from index 5 to index 0 in reverse order by writing something like this 6 of 5 is 2 0 is 2 minus 1 and yes element at index 0 will not be fetched there is a function called reversed which helps in iterating a list in reverse order let's try that out for I in reversed of list 6 we will print I basically we can observe that we are able to iterate the list in reverse order like 20 18 16 etc let's see some of slicing examples so list 6 of is 2 file so if we skip the starting index it will index from it will slice from index 0 so in our example elements from index 0 to index 4 will be fetched list 6 of 5 is 2 so if we skip the end index everything from the starting index to the last next will be fetched even last element will be fetched next we shall look at slicing assignment or slice assignment slice assignment is used to insert or replace items of 1 list inside another list added any given particular location let's say we want to insert the list 10 comma 20 starting from index 1 of list 6 so we can write list 6 of 1 is to 1 equal to 10 comma 20 so we can observe that 10 comma 20 is added starting from index 1 in the next example we shall replace items at index 1 comma 2 with values 30 and 40 so we have to write list 6 of 1 is 2 equal to 30 comma 40 so we can observe that 10 comma 20 is replaced with 30 and 40 next we will remove elements 30 and 40 using slicing assignment so to do this we need to simply write list six off one is to three equal to empty list so the elements at one and two will be removed here next we shall understand how to copy list copying off list is somewhat tricky we shall understand why so step-by-step let's assume we had a simple list a equal to 1 comma 2 comma 3 so if we want to make a copy of this list and assign it to another variable D we can simply write B equal to P so B also has the same items as a so this is fine now let's try to alter the first element of a by writing a of 0 equal to 20 now we can see that the values of a has changed to 20 comma 2 comma 3 but let's just try to check the value of B it should still be 1 comma 2 comma 3 right so let's print B you can see that even the value hat is 20 comma 2 comma 3 but why did this happen actually if you were right B equal to a 2 copy the list a you're not copying the values of list a to List B you're basically copying the address of list a to B so a and B both are pointing to the same list so we shall look at how to copy the list values let's create the list a again a equal to 1 comma 2 comma 3 so in order to copy values of list A to B we should write B equal to a list of a so even if we change the values of list a now the values of B will remain same so that there is one more problem with this approach let's try to understand what that is let's assume we have a list of lists let's create one a equal to 1 comma 2 comma 3 is 1 list and 4 comma 5 is another list a is basically having two lists inside main list so let's copy this list to be using be equal to list of it now let's try to change a value of first inner list by writing a of 0 0 equal to qualified and we can see that the value of first inner list in a is change now let's try to print B we can see that the value of first list in B is also changed so basically the copy that we made failed if the list has list of items inside it to solve this we need to deep copy the list so let's see how we can deep copy a list so let's create or a list again which contains two lists inside it so a equal to 1 comma 2 comma 3 in one list and 4 comma 5 in the second list so in order to pop deep copy a list we have to import a model called copy now let's copy the list a list be using deep copy so now let's try changing an element in first inner list of a a of 0 0 equal to 25 let's print a and B so we can observe that only a has changed now let's understand how to use for loop spin on lists let's create a list k equal to cat dog duck and parrot basically we can look through in two ways let's see the first one for I in a print I it's basically we are looping each item I in the list and simply printing it the second method is to loop through the index of our list so we can write for I in range 0 comma length of a print index and value so we can so once you have the index I you can get the value in that index using a of I [Music] let's see how to join all the contents in the list into a string so let's create a list LST equal to we have a hello comma world come up Python is awesome so in order to join each word in this list with a space in between we can write LST is TR equal to in between the double quotes we need a space and dog join LST let's print our string so yeah instead of space let's say you want to join with hyphen in between we can write LST STR to equal to in between the double quotes you can specify - and dog join LST next let's see how to convert a list of Lists into a flat list let's create a list of lists list of Lists equal to 1 comma 2 comma 3 and 4 comma 5 comma 6 same second list and 7 in our one list and 8 come on the 9 into the final list so in order to convert it into a flat list we can use a model called inter tools let's import little tool and convert our list of Lists into a flat list using a flat list equal to list of itertools dove chain and you have to write a star list of lists so we can see that our list of Lists have been converted to a flat list next let's see if we if list has duplicate elements let's create a list LST equal to 1 comma 2 comma 3 3 4 5 5 & 6 6 6 so let's print all the items which are repeating to do this we can write I for I in LSD if LSD dot count of I is greater than 1 so basically we are just filtering those items which are repeating or in other words meaning they're count is more than one so if you want to see the unique values we can convert the list to set using unique equal to a set of olestra to so basically set will only contain unique items so if you need to convert it back to a list we can simply write unique equal to list of illustrative next we shall see how to shift or rotate our list let's create a list n st 2 equal to 1 comma 2 comma 3 so in order to rotate our list we can do so by importing a model called DQ inside collections so we shall convert our list to DQ first so LST 2 equal to DQ of LST - let's try to rotate our list to the right by writing LST 2 dot rotate 1 so we can observe that items are rotated to the right by 1 so similarly for left shift we can write LST dot rotate -1 so yeah we can convert the DQ back to list by simply writing LST 2 equal to list of illustrative [Music]
Info
Channel: Art of Engineer
Views: 98,314
Rating: 4.8713884 out of 5
Keywords: python list, python list tutorial, python tutorial, list tutorial python, list comprehension, List of numbers using range, Built in functions for lists in python, slicing and indexing in python, how to copy list, shift or rotate the list, for loops on lists, slice assignment list, create a python list, join all the contents in the list in to a string, Creating List from comma separated strings, Convert the dictionary key or value to list, lists in python
Id: 9rLdQP3g4fw
Channel Id: undefined
Length: 28min 4sec (1684 seconds)
Published: Fri Jul 06 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.