From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
00:00 Hello friends and welcome to the tutorial on "Getting started with lists".
00:06 In this tutorial we will be getting acquainted with a python data structure called lists. At the end of this tutorial, you will be able to,

Create lists Access list elements Append elements to lists Delete elements from lists

00:23 So now, List is a compound data type, it can contain data of mutually different data types.
00:32 List is also a sequence data type where all the elements are arranged in a specific order.
00:36 Start the ipython interpreter and first create an empty list with no elements.
00:41 In command ipython.Then type empty is equal to square brackets.
00:53 Then type empty() within brackets empty.
00:59 This is an empty list without any elements.
01:03 Lets define a non-empty list as non empty is equal to within square brackets and in single quotes spam comma within single quotes eggs comma 100 comma 1.234]
01:24 Thus, the simplest way of creating a list is typing out a sequence of comma-separated values (or items) between two square brackets.
01:34 As we can see, lists can contain different kinds of data.
01:37 In the previous example 'spam' and 'eggs' are strings whereas 100 and 1.234 are integer and float respectively.
01:48 Thus, we can put elements of different data types in lists including lists itself.
01:54 This property makes lists heterogeneous data structures.
01:59 Let us include a list within a list.
02:02 We access an element of a list using its corresponding index.
02:07 So type in command non empty,then type listinlist is equal to square brackets [4,2,3,4],'and', 1, 2, 3, 4.
02:41 So now we can access an element of a list using corresponding index.
02:49 Index of the first element of a list is 0.
02:53 So for the list nonempty, nonempty[0] gives the first element, nonempty[1] the second element and so on and nonempty[3] the last element.So type in command non empty within [0] then again nonempty[1] then nonempty[3]
03:23 Pause the video here, try out the following exercise and resume the video.
03:28 What happens when you do nonempty[-1].
03:34 YOu can switch to terminal and type nonempty -1 in square brackets.
03:44 As you can see you get the data of last element which is 1.234.
03:53 In python negative indices are used to access elements from the end.
03:58 So, -1 gives the last element which is the 4th element , -2 gives second element to last and -4 gives the fourth from the last which, in this case, is the element first .
04:15 So type nonempty -1 nonempty[-2] then nonempty -4.
04:24 We can also append elements to the end of a list using the append function.
04:28 So type nonempty dot append within brackets and single quotes onemore.
04:41 We can see an error
04:46 Then type nonempty ; then type nonempty dot append within brackets 6.
05:09 then again nonempty.
05:15 Please, pause the video here, do the exercise and then continue.
05:21 What is the syntax to get the element 'and' in the list,listinlist ?
05:29 How would you get 'and' using negative indices?
05:34 So, the solution is on your screen
05:38 As we can see nonempty is appended with 'onemore' and 6 at the end.
05:45 Let us move further.
05:47 We can use len function to check the number of elements in the list.
05:50 Let us find out the length of the list 'nonempty'.
05:54 So type in command len within brackets nonempty .
06:05 Just like we can append elements to a list, we can also remove them.
06:08 There are two ways of doing it. One is by using index.We will try let type del{nonempty[1]}
06:12 So type del within brackets nonempty and square brackets 1.
06:26 The function del deletes the element at index 1, i.e the second element of the list, 'eggs'.
06:34 The other way is removing element by content.
06:37 Lets say one wishes to delete 100 from nonempty list.
06:41 For this, one could use the function remove. So type nonempty.remove (100)
06:55 But what if there were two 100's.
06:57 To check that lets do a small experiment.
07:01 Now, in command nonempty dot append within brackets in single quotes spam.
07:11 Then type nonempty.
07:15 Then type nonempty dot remove within brackets in single quotes spam; then type nonempty for the output.
07:29 If we now check, we will see that the first occurrence 'spam' is removed and therefore the function remove removes the first occurence of the element in the sequence and leaves others untouched.
07:39 One should remember this, that while del removes by index number, remove removes on the basis of content being passed on.
07:50 Let us take an example.
07:53 Type del gives us [1,2,3].
07:59 So type on the terminal k is equal to 1,2 ,1,3 and then type del within brackets and square brackets k then square brackets 2.
08:25 remove will give us [2,1,3].
08:29 Since it deletes the first occurrence of what is returned by x[2] which is 1.
08:37 So type k dot remove and in brackets x then square brackets 2.
08:59 As we have seen an error we have to change x of 2 to k of 2.
09:09 Pause the video here, try out the following exercise and resume
09:14 Remove the third element from the list, listinlist.
09:19 Remove 'and' from the list, listinlist.
09:24 The solution is on your screen.
09:29 This brings us to the end of this tutorial. In this tutorial, we have learnt to, Create lists, Access lists using their index numbers, Append elements to list using the function append
09:40 Delete Element from lists by specifying the index number of the element to be deleted in the del function.
09:47 Then,Delete element from list by content using remove function and final one is Find out the list length using len function.
09:59 Here are some self assessment questions for you to solve
10:02 How do you create an empty list?
10:05 Can you have a list inside a list ?
10:09 How would you access the end of a list without finding its length?
10:15 And the answers,
10:18 We create an empty list just by leaving the space inside the square brackets empty.empty=[]
10:30 Yes.
10:34 List can contain all the other data types, including list.
10:44 Here is an example list_in_list=[2.3,[2,4,6],'string,'all datatypes can be there']
10:56 Using negative indices, we can access the list from the end using negative indices.
11:04 This is an example nonempty = ['spam', 'eggs', 100, 1.234] nonempty[-1]
11:15 Hope you have enjoyed this tutorial and found it useful.
11:19 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha