|00:01||Hello Friends. Welcome to the tutorial on "Getting started with lists".|
|00:07|| At the end of this tutorial, you will be able to,
Access list elements
Append elements to lists and
Delete elements from lists
|00:21||To record this tutorial, I am using
Ubuntu Linux 14.04 operating system
|00:35||To practise this tutorial, you should know how to
run basic Python commands on the ipython console
If not, see the relevant Python tutorials on this website.
|00:48||What is a List?
A List can store a sequence of elements.
All elements need not be of the same data types
|00:59||Let us open the Terminal by pressing Ctrl+Alt+T keys simultaneously|
|01:06|| Now, type ipython3 and press Enter. We are now in the ipython prompt.
Let us clear the terminal by pressing Ctrl + L for better view.
|01:20||Let us define a list with a variable name mylist|
mylist equal to inside square brackets inside quotes spam comma inside quotes eggs comma hundred comma one point two three four and press Enter.
|01:45||In Python, strings are always placed inside single or double quotes.|
|01:52||Now enter type within brackets mylist and press Enter.|
|02:01|| Here type is a function which will return the datatype of the variable.
mylist is a list datatype.
|02:10|| As we can see, lists can contain elements of different datatypes
In mylist, spam and eggs are strings, whereas hundred and one point two three four are integer and float respectively.
|02:27||In the code typed earlier, the name mylist is a variable.|
|02:32||In Python, a variable must either start with an alphabet or an underscore.|
|02:39||They cannot start with numbers and cannot be the same as Python keywords.|
|02:45||You will learn about keywords as we proceed.|
|02:49||Few examples of keywords are “for, if, else, elif, while, in, def, or, and”.|
|03:00||A variable name cannot have spaces or punctuation characters or any arithmetic characters.|
|03:07||These are some of the valid and invalid variable names.|
|03:14|| Getting back to our lists, the list could be created as num underscore list is equal to inside square brackets '''one comma two' and press Enter
|03:32||x equal to inside square brackets inside quotes a comma inside quotes b and press Enter
Any valid variable name could be used.
|03:50||Let us now create an empty list with no elements.|
|03:55||Type myemptylist is equal to open and close square brackets and press Enter.
This is an empty list without any element
|04:08||We can access an element of a list using its corresponding index or position.|
|04:15||Index value of the elements starts from 0, 1, 2 and so on.|
|04:22||Negative indices are used to access elements from the end. Which starts from -1, -2, and so on|
|04:33||The syntax to get a specific list element is
variable inside square brackets elements index value
|04:44||Switch to the terminal.|
|04:47|| Type mylist inside square brackets zero and press Enter.
As we can see mylist gives the first element spam
|05:00||Type mylist inside square brackets one and press Enter.
mylist gives the second element.
|05:11|| Pause the video. Try this exercise and then resume the video.
What happens when you type mylist[-1].
|05:21||Switch to the terminal for solution|
|05:25||Type mylist inside square brackets minus one and press Enter.
As you can see you get the last element which is one point two three four.
|05:39||We can also create a list inside a list.|
|05:43||This property makes lists heterogeneous data structures. This is the syntax for list inside a list.|
|05:53||Let us include a list within a list for the variable doublelist.|
|05:59||Type the code as shown here: Here b c d is a list in list which is represented within square brackets inside a list.|
|06:11|| Now let us fetch some element from doublelist.
Type: doublelist inside square brackets one and press Enter
|06:23||We can see the output as b c d within square brackets. This is because the index value one contains the list inside a list as its element.|
|06:37||Now let us try to access a specific element, say b from the list inside a list.|
|06:45||For this type: doublelist inside square brackets one inside square brackets zero and press Enter.|
|06:57||Here the 1st parameter represents the index of the list in the main list.|
|07:04||The second parameter represents the index value of b in the list inside the list.|
|07:12|| Pause the video. Try this exercise and then resume the video.
doublelist=[‘a’, [‘b’,’c’,’d’], 'and', 5, 6, 7, 8]
|07:18||What is the command to get the element 'and' in the list doublelist?|
|07:24||How would you get 'and' using negative indices?|
|07:28||How would you get element 'd' from the list doublelist?|
|07:33||The solution is on your screen.|
|07:38|| We can use len function to check the number of elements/length of in the list.
The syntax is: len inside parentheses variable
|07:49||Let us find the number of elements in mylist which we created earlier.|
|07:55||Switch to the terminal and clear the screen. Recall mylist by pressing up arrow key and press Enter.|
|08:05||Type len inside parentheses mylist and press Enter. We get the length of mylist as four.|
|08:16||We can append elements to the list using the append function.|
|08:21||This function will add the element to the end of the list.|
|08:26||The syntax is: variable dot append within parentheses element to be added.|
|08:35|| Switch to the terminal
mylist dot append inside parentheses within quotes sharp and press Enter.
|08:49||Type mylist dot append inside parentheses six and press Enter.|
|08:57||Let us check the updated mylist. Type mylist and press Enter We can see mylist is appended with sharp and six at the end.|
|09:10||We can also remove elements from lists.
There are two ways of doing it. One is by using the index with del keyword.
Syntax is del variable inside square brackets element’s index value.
|09:29||The other way is removing element by the value using remove function
Syntax is variable dot remove inside parentheses element to be removed
|09:43|| Switch to the terminal.
Type mylist and press Enter to see the updated list.
Type del space mylist inside square brackets one and press Enter.
|10:01||The keyword del deletes the element at index one, i.e the second element of the list, eggs.|
|10:11||Again type mylist and press Enter. We can see that the element eggs has been deleted from the list.|
|10:22||Let us delete the element hundred from mylist list.|
|10:27||For this we can use the function remove. Type mylist dot remove within parentheses hundred and press Enter.
Let us check the updated mylist.
|10:44||What if the elements are repeated?|
|10:47||To check that, let’s do a small experiment.Type mylist dot append within parentheses within quotes spam and press Enter|
|11:01||Type mylist and press Enter Here is the updated mylist.|
|11:08||It contains the element spam twice. One is at the start and the next is at the end of the list.|
|11:17|| Now remove the element spam by typing: mylist dot remove inside parentheses inside quotes spam and press Enter.
Let us check updated mylist.
|11:35||We can see that only the first occurrence of 'spam' has been removed.|
|11:41||The function remove removes the first occurrence of the element in the sequence.
Remember, the del keyword removes the element by index value. remove function removes on the basis of element being passed on.
|11:57||Using remove function, we can also remove an element with its index value.|
|12:03||Let us try this with an example. Type k is equal to inside square brackets one comma two comma one comma three and press Enter|
|12:17||Then type k dot remove inside parentheses k inside square brackets two and press Enter Type k and press Enter We can see the output as[2,1,3].|
|12:36||Since it deletes the first occurrence of what is returned by k inside square brackets two which is one.|
|12:45|| Pause the video. Try this exercise and then resume the video.
1. Delete the fourth element from the list doublelist.
2. Remove 'and' from the list doublelist.
|12:58||The solution is on your screen.|
|13:04||This brings us to the end of this tutorial.
In this tutorial, we have learnt to create:
List with elements
List within a list
|13:18||We also learnt to,
Find out the list length using len function.
Access elements using their index numbers.
|13:28||Append elements to list using the function append.
Delete element from list using the del and remove function
|13:37||Here are some self assessment questions for you to solve
How do you create an empty list?
Can you have a list inside a list?
How would you access the last element of a list without finding its length?
|13:54|| And the answers,
1. We create an empty list just by leaving the space inside the square brackets empty. myemptylist equal to open and close square brackets.
|14:07||2. Yes, list can contain all the other data types, including list.
3. Using negative indices, we can access the last element from the list.
|14:19||Please post your timed queries in this forum.|
|14:23||Please post your general queries on Python in this forum.|
|14:28||FOSSEE team coordinates the TBC project.|
|14:33||Spoken-tutorial is funded by NMEICT, MHRD, Govt. of India. For more details, visit this website.|
|14:44||This is Trupti Kini from IIT Bombay signing off. Thank you.|