Practical -Python fundamentals - working with list and tuple

16 minutes
Share the link to this page


In this video, I'm going to show you how to work with lists and tuples in Python. However, let's make a review of the third lesson. In the previous lesson, I showed you that Python is a language that supports object oriented programming. There is a data constructor called string, which is a class that contains many methods. There is a particular method called split that returns a list of sub strings separated by delimiters. In this example, we use commas.

So open your idle python shell, write the string x dot y dot set and hold it in a variable called s one After you set the variable called list one, that is s one dot split in a pair of parentheses, that enclose the string dot will save the outcome in the variable list one. When you print this, you will get the list of three letters x, y, and Zed as I show you in the video. Now suppose you want to change the second string, so access it that is list one, parenthesis one, and then set it to the new value, in this case, equal to number five. After that, press enter and print the list again to check the outcome. Now create a new string that contains the phrase, Python is a piece of cake and assign it to the variable s to use the split method, but in this case, use the space as delimiter.

Set a variable called list to, to the outcome of the splitting. How many elements does this have? If you don't remember how to get the length of the sequence structure, you may use the length function. That is right. l e n, open parenthesis, right list to close parenthesis and press enter. The outcome will be six, which is the number of words that are in the phrase I told you that Python handles different type of data structures, list tuples and dictionaries.

But what is the difference? list may hold some number element, others string or even other lists. Python uses Zero Based indexing to access to the information of each element using the bracket syntax. You can also change the value of each element or the length of the list either by adding or removing elements. A tupple is likewise a list, but their values are immutable, so you cannot change it after you set the value of the tupple. The dictionary is similar to a list But you get the access by keys.

This key can be a number or a string. We will look at some examples later. First, we're going to work on lists. To create an empty list, write list three, equal a pair of brackets and press enter. With length function, check that this does not have elements, the result will be zero. Okay, now, how can I append a new element or a list in the empty list?

You have to use the append method. So write list three dot append, open parenthesis, right for closed parenthesis and press enter. You'll notice that you have the number four as the first element of the list. Now create a list of image file formats. In this case, use the first string with values JPGTFB, M, P and P and G. Set the variable called list for to the list of formats. Hence, right list for equal open brackets and write each string separated by commas.

After that, close the bracket and press enter. Print list for and observe the outcome. What would happen if you run the append method on list three To add the list four, let's try it. So write list three dot, append, in parenthesis, write list four, and press enter. Then print list three. What did you get?

If you access the second element of the list three, you get the list of formats. Keep in mind that list three has only two elements. One is number and the other is a list. How can I access an element of the list of format through the list three, first, access the second element of the list three Write list three, open bracket, write one, close bracket. After that, open a new bracket and write the correct index number to access the element, in this case, the third element. So write to close brackets and press enter.

The outcome would be the string b m p. Now, imagine that you need to extend the list of formats with a new extension. G, F, and s VG. You have to use the extend method. So write list for dot extend, open parenthesis, create a list with the strings g x F and s v g, then close the parenthesis and press enter. Check how many elements does this have now, print list for and observe the outcome. Notice the list has the two elements at the end of the list.

Keep in mind the difference between append and extend methods. If you want to add one by one, you have to use append. However, if you want to extend it by many elements, you have to use extend. In either case, it will amend append at the end of the list. Remember the changes of list four will affect the elements and values of The second element of list three. What happens if you want to add a new format, b, p g, in the third element of list four.

In this case, you can use the append method, since we're going to add it in the first position of the list and not at the end. So you have to use insert method, right list for dot, insert, open parenthesis, write two, comma bPg, close parenthesis, and press enter. Then print list four, and observe the result. Notice the extension. b p, g appears before B M p at the first position of the list. On the other hand, if you want to get rid of an element of the list, you have to use the Remove method.

So, suppose that you need to remove the G, F extension from list four. In this case, write lists for dot remove, open the parenthesis, write the string, G, i f, close parenthesis, and press enter. Then print list for and check if the element was removed. This method will remove only the first element which is equal to the string g i f. We can also find the index number the values For example, if you want to know the index of the string b, m, p, you have to write list for dot index, open parenthesis, right, b, m, p, close parenthesis and press enter. The outcome in the shell is three. So the string b m, p is the fourth element of the list.

Moreover, you can use the index number to remove an element by using the pop method, right list dot pop, open parenthesis, write the index of the element that you want to remove. Then close parenthesis and press Enter In this case, if you use index one, you remove the string, T i FF from the list. You can also use the del function to get rid of a variable or an element of the list. Right Dell list one, you will delete the variable list one. However, if you write Dell list to open bracket, write one, close bracket and press enter. You will delete the word is from the list.

Besides, if you want to delete a sub list, remember to use this syntax properly. So write down list to open bracket Get colon and the end index. You can omit the start index or the end index to observe the effects of this statement. Furthermore, if you want to order your list, use the sort method, write list for dot sort and a pair of parenthesis. Then press enter and press the list for to observe the changes. If the list holds, numeric and string elements, it will order first the numeric values and then the strings.

Keep in mind that after you call the sort method, the elements would maintain this order. Now, we are going to work with couples. Remember that they are in immutable. So, you cannot change their value and link this kind of a structure is used to return multiples value from functions. So you have to treat them as containers. Let's make some examples.

First write T one equal open parenthesis, write the string x comma string y comma string that closed parenthesis and press enter. Then print T one, what did you get? What is the difference between a list and a tupple? You can differentiate them from each other, if you looked at the outside delimiters hence, if they are bracket it is other list, but if there are open parenthesis, it is a tupple you can access the element of the tupple. As you do it with the list, try to modify the value of the second element of the tupple. Like with the list.

When you do that, you will get a type error. Since the object doesn't support item assignment, if you want to create a tupple of one element, you have to write the element followed by a comma and closed in parenthesis. As I show you in the video. Also, you can create tupple without using parenthesis, try to create a tupple with one element without using parenthesis, did you get an error, print the outcome and you will see that the outcome delimiters are parenthesis. So it's a tupple. That's all for this tutorial.

I hope you liked it. And you want to watch more tutorials about Python and je s

Sign Up


Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.