Python-3.4.3/C3/Manipulating-lists/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Visual Cue
Narration
Show Slide Title Welcome to the spoken tutorial on Manipulating Lists.
Show Slide

Objectives

In this tutorial, we will learn about
  • slicing and striding of lists
  • Sort and reverse lists.


Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3
  • IPython 5.1.0


Show Slide

Pre-requisites

To practise this tutorial, you should know how to
  • run basic Python commands on the ipython console and
  • use lists

If not, see the relevant Python tutorials on this website.

We have already learnt about list and how to access individual elements in the list.
Show Slide


Slicing

Now we will see about slicing of lists.


The syntax for slicing is p inside square brackets start colon stop.


It returns all the elements of p between start and stop values.


The element with the stop index value will not be included.

Open the terminal Let us start ipython.


Open the terminal.

Type ipython3


Press Enter

Type ipython3 and press Enter.


From here onwards, remember to press the Enter key after typing every command on the terminal.

Type

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

Let us understand the slicing with an example.


Type, primes is equal to then type as shown.

Type

primes[4:8]

Now we will try to obtain all the primes between 10 and 20 from the above list of primes.


Type, primes inside square brackets 4 colon 8


Recall that the start index value is 0.


Observe that the first element we want is 11 which has index 4 in the list.


So, start value is 4.


Also, 19 is the last element we require which has index 7 in the list.


The element with index equal to stop value will not be included.


So, end value is 8.

Pause the video.


Try this exercise and then resume the video.

Show Slide Exercise 1 Obtain the primes less than 10, from the list primes.
Switch to terminal Switch back to terminal for the solution
Type

primes[0:4]

Type, primes inside square brackets 0 colon 4


Observe that in this case, our start value is 0.


7 is the last element we require which has index 3 in the list.


So, end value is 4.


<<PAUSE>>

Type num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] Next we will learn to use step value in slicing.


Type 0 to 13 in a list and assign it to a variable num as shown.

Type

num[1:10:2]


Press Enter

Let us say, we want all the odd numbers less than 10 from the list num.


We will specify the step value in which the slice must be obtained.


Type num inside square brackets 1 colon 10 colon 2


We have to start from element with index 1 upto index 10 in steps of 2.


It is called striding of list.


Press Enter to get the output.


We got the odd numbers less than 10.

Type

num[:10]


When no step is specified, it is assumed to be 1.


Similarly, there are default values for start and stop as well.


Type, num inside square brackets colon 10


It gives the first 10 elements of the list.


If we don't specify the start value, the first element of the list is taken as starting.

Type

num[10:]


Type, num inside square brackets 10 colon


This gives us all the elements from 10th element to the end.


If we don't specify the stop value, the elements till the last index of the list will be returned.


Start or end value can be negative to indicate that they are counted from the end of the list.

Type

num[::2]

Next let us get all the even numbers in the list "num"


Observe that all the even numbers are at even index locations.


So, type num inside square brackets colon colon 2


We got all the even numbers.


This is called striding.

Pause the video.


Try this exercise and then resume the video.

Show Slide Exercise 2 Obtain all the multiples of three from the list num.
Switch to terminal Switch back to terminal for the solution
Type, num[::3] Type, num inside square brackets colon colon 3


It gives us every third element from the list (i.e) multiples of 3.


<<PAUSE>>

Type a = [5, 1, 6, 7, 7, 10] Next let us learn to sort a list.


Type, a is equal to inside square brackets 5, 1, 6, 7, 7, 10

Type, a.sort() sort method is used to sort a list.


Type, a.sort open and close brackets

Type

a

Type, a to get the output.


We can see that the contents of the list a is sorted now.

Slide:

Sorted()

* Python provides a built-in function called sorted.
  • sorted function sorts the list which is passed as an argument to it.
  • It returns a new sorted list.


Type a = [5, 1, 6, 7, 7, 10] Again we will assign the same value to a as shown.
Type sorted(a) Type, sorted inside brackets a
Type sa = sorted(a) We can store this sorted list into another list variable sa.


Type, sa is equal to sorted inside brackets a

Type sa To see the sorted list, type sa
Type, r = [1, 2, 3, 4, 5]


Python also provides the reverse method which reverses the list in place.


Type, r is equal to inside square brackets 1, 2, 3, 4, 5

Type r.reverse() r.reverse open and close brackets
Type r To see the list r, type, r


We got the reverse of the list.


The original list r is changed now.

Type, r = [1, 2, 3, 4, 5] To reverse a list, we can also use striding with negative values.


Again we will assign the same value to r as shown.

Type r[::-1] Type, r inside square brackets colon colon minus 1
Type ra = r[::-1] We can also store this new reversed list in another variable.


Type, ra is equal to r inside square brackets colon colon minus 1

Type ra To see the reversed list, type ra
Pause the video.


Try this exercise and then resume the video.

Show Slide

Exercise 3


Given below is the list of marks of a student in an examination.


Obtain a list with marks in descending order.

Switch to the terminal Switch back to the terminal for the solution.
Type marks = [99, 67, 47, 100, 50, 75, 62] Type the list of marks as shown.
Type,

sorted(marks)[::-1]

To get the marks in descending order type,

sorted inside brackets marks inside square brackets colon colon minus 1

Type

sorted(marks, reverse = True)

We can also get the same output by typing,

sorted inside brackets marks, reverse equal to True


<<PAUSE>>

Show Slide

Summary slide


This brings us to the end of this tutorial.


Let us summarize.


In this tutorial, we have learnt to,

  • Obtain the parts of lists using slicing and striding.
  • Sort lists using the sort method.
  • Use the method reverse to reverse the lists.


Show Slide

Evaluation


Here are some self assessment questions for you to solve


  1. Given the list primes. How do you obtain the last 4 prime numbers?
  2. Given a list, p, of unknown length. Obtain the first 3 characters of it.


Show Slide

Solutions


And the answers,
  1. The last four primes can be obtained from the given list as,

primes inside square brackets minus 4 colon

  1. The first 3 characters can be obtained as,

p inside square brackets colon 3

Show Slide Forum Please post your timed queries in this forum.
Slide Fossee Forum Please post your general queries on Python in this forum.
Slide Textbook Companion FOSSEE team coordinates the TBC project.
Show Slide

Acknowledgement

Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

Show Slide Thank You This is Priya from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Priyacst