Python/C3/Getting-started-with-lists/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Visual Cue Narration
Show Slide 1

Containing title, name of the production team along with the logo of MHRD

Hello friends and welcome to the tutorial on "Getting started with lists".
Show Slide 2

Learning objectives

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,
  1. Create lists
  2. Access list elements
  3. Append elements to lists
  4. Delete elements from lists


ipython
empty = []
type(empty)
List is a compound data type, it can contain data of mutually different data types. List is also a sequence data type where all the elements are arranged in a specific order.

Start the ipython interpreter and first create an empty list with no elements.

nonempty = ['spam', 'eggs', 100, 1.234] This is an empty list without any elements.

Lets define a non-empty list as:

listinlist=[[4,2,3,4],'and', 1, 2, 3, 4] Thus, the simplest way of creating a list is typing out a sequence of comma-separated values (or items) between two square brackets.

As we can see, lists can contain different kinds of data. In the previous example 'spam' and 'eggs' are strings whereas 100 and 1.234 are integer and float respectively. Thus, we can put elements of different data types in lists including lists itself. This property makes lists heterogeneous data structures.

Let us include a list within a list.

nonempty[0]
nonempty[1]
nonempty[3]
We access an element of a list using its corresponding index. Index of the first element of a list is 0. 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.
Show Slide 3

Assignment 1

Pause the video here, try out the following exercise and resume the video.

What happens when you do nonempty[-1].

Switch to the terminal
nonempty[-1]
As you can see you get the last element which is 1.234.
nonempty[-2]
nonempty[-4]
In python negative indices are used to access elements from the end. -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 first element.
nonempty.append('onemore')
nonempty
nonempty.append(6)
nonempty
We can also append elements to the end of a list using the append function.
Show Slide 4

Assignment 2

Please, pause the video here. Do the exercise and then continue.
  1. What is the syntax to get the element 'and' in the list,listinlist ?
  2. How would you get 'and' using negative indices?


Show Slide 5

Solution 2

The solution is on your screen

listinlist[1] listinlist[-5]

As we can see nonempty is appended with 'onemore' and 6 at the end.

len(nonempty) Let us move further. We can use len function to check the number of elements in the list. Let us find out the length of the list 'nonempty'.
del(nonempty[1]) Just like we can append elements to a list, we can also remove them. There are two ways of doing it. One is by using index.
nonempty.remove(100) The function del deletes the element at index 1, i.e the second element of the list, 'eggs'.

The other way is removing element by content. Lets say one wishes to delete 100 from nonempty list.For this, one could use the function remove.

nonempty.append('spam')
nonempty
nonempty.remove('spam')
nonempty
But what if there were two 100's. To check that lets do a small experiment.
If we now check, we will see that the first occurence 'spam' is removed and therefore the function remove removes the first occurence of the element in the sequence and leaves others untouched.

One should remember this, that while del removes by index number, remove removes on the basis of content being passed on. Let us take an example.

k = [1,2,1,3]
del([k[2])
del gives us [1,2,3].
k.remove(k[2])
k
remove will give us [2,1,3]. Since it deletes the first occurrence of what is returned by k[2] which is 1.
Show Slide 6

Assignment 3

Pause the video here, try out the following exercise and resume the video.
  1. Remove the third element from the list, listinlist.
  2. Remove 'and' from the list, listinlist.


Show Slide 7

Assignment 3

The solution is on your screen.

del(listinlist[2]) listinlist.remove('and')

Show Slide 8

Summary

This brings us to the end of this tutorial. In this tutorial, we have learnt to,
  1. Create lists.
  2. Access lists using their index numbers.
  3. Append elements to list using the function append.
  4. Delete Element from lists by specifying the index number of the element to be deleted in the del function.
  5. Delete element from list by content using remove function.
  6. Find out the list length using len function.


Show Slide 9

Self assessment questions slide

Here are some self assessment questions for you to solve
  1. How do you create an empty list?
  2. Can you have a list inside a list ?
  3. How would you access the end of a list without finding its length?


Show Slide 10

Solution of self assessment questions on slide

And the answers,
  1. We create an empty list just by leaving the space inside the square brackets empty.
    empty=[]
  2. Yes.List can contain all the other data types, including list. Here is an example
    list_in_list=[2.3,[2,4,6],'string,'all datatypes can be there']
  3. Using negative indices, we can access the list from the end using negative indices. This is an example
    nonempty = ['spam', 'eggs', 100, 1.234]
nonempty[-1]


Show Slide 11

Acknowledgment Slide

Hope you have enjoyed this tutorial and found it useful. Thank you!

Contributors and Content Editors

Chandrika