1 of 33

LISTS

2 of 33

  • set of values enclosed in square brackets [].
  • Values in the list can be modified, i.e. it is mutable.
  • use index in square brackets [] to identify a value belonging to it.
  • The values that make up a list can be of any type.

INTRODUCTION

3 of 33

  • For accessing an element of the list, indexing is used.

Syntax is :

Variable name [index]

Index - integer value (+ve / -ve)

Positive value - counting forward from beginning of the list

Negative value - counting backward from end of the list.

INTRODUCTION

4 of 33

5 of 33

A list containing another list known as nested list.

LISTS EXAMPLE

6 of 33

Use double subscript to fetch the sub list in a given list.

LISTS EXAMPLE

Similarly, L2[2][0] will fetch you 4 value

L2[2][2] will fetch you 7 value.

7 of 33

STATE DIAGRAM OF LIST

>>> L1 = [45,56,67,20]

State Diagram would be:

Forward Indexing

Backward Indexing

0

1

2

3

-4

-3

-2

-1

45

56

67

20

LIST INDEX

LIST elements

8 of 33

LIST - Example

9 of 33

CREATING MULTIPLE LISTS – LIST MAPPING

Creating Multiple List using assignment statement

Creating a list and mapping to other lists.

10 of 33

LIST CONCATENATION

A and B are two different LISTS and third LIST is created by combining both A and B.

 

11 of 33

12 of 33

Deleting Elements of LIST

It is possible to delete/remove element(s) from the list. There are many ways of doing so:

(i) If index is known, we can use pop ( ) or del

(ii) If the element is known, not the index, remove ( ) can be used.

(iii) To remove more than one element, del ( ) with list slice can be used.

(iv) Using assignment operator

13 of 33

Deleting Elements of LIST

1. pop ( ) Method

2. del Method

3. remove ( ) Method

14 of 33

Deleting Elements of LIST

1. pop ( ) Method

It removes the element from the specified index, and also return the element which was removed.

Its syntax is:

List.pop ([index])

15 of 33

Deleting Elements of LIST

1. pop ( ) Method

For Example:

Popped element 67 is assigned to x

16 of 33

Deleting Elements of LIST

2. del Method

del removes the specified element from the list, but does not return the deleted value.

Syntax :

del Listobject[index]

del method example

17 of 33

Deleting Elements of LIST

3. remove ( ) Method

In case, we know the element to be deleted not the index, of the element, then remove () can be used.

Syntax is:

listobject.remove(value)

>>>L1.remove(60)

contd…

18 of 33

Deleting Elements of LIST

3. remove ( ) Method

19 of 33

SOME MORE METHODS

20 of 33

SOME MORE METHODS

1. insert () Method

2. reverse ( ) Method

3. sort ( ) Method

4. clear ( ) Method

5. index ( ) Method

6. extend ( ) Method

21 of 33

OTHER METHODS

1. insert () Method

This method allows us to insert an element, at the given position specified by its index, and the remaining elements are shifted to accommodate the new element. insert () requires two arguments-index value and item value.

Its syntax is

list. insert (index, item)

22 of 33

OTHER METHODS

1. insert () Method

For Example:

23 of 33

OTHER METHODS

2. reverse ( ) Method

This method can be used to reverse the elements of the list in place

Its syntax is:

list.reverse ( )

Method does not return anything as the reversed list is stored in the same variable.

24 of 33

OTHER METHODS

2. reverse ( ) Method

For Example:

25 of 33

OTHER METHODS

3. sort ( ) Method

For arranging elements in an order Python provides a method sort ( ) and a function sorted ( ). sort ( ) modifies the list in place and sorted ( ) returns a new sorted list.

26 of 33

OTHER METHODS

3. sort ( ) Method

For Example:

Default Ascending

Order sorting

27 of 33

OTHER METHODS

3. sort ( ) Method

For Example:

Descending order

28 of 33

OTHER METHODS

3. sort ( ) Method

For Example:

Sorting using key value (length of string)

29 of 33

OTHER METHODS

4. clear ( ) Method

clear() method deletes

or clears the content of list. For Example:

30 of 33

OTHER METHODS

5. index ( ) Method

To know the index of an

element in a given list

For Example:

31 of 33

ABOVE AVERAGE PROGRAMS ON LISTS

8. Write a Python program to print the numbers of a specified list after removing even numbers from it.

def num_prn():

num = [7,8, 120, 25, 44, 20, 27]

num = [x for x in num if x%2!=0]

print(num)

32 of 33

Tuples in Python

Tuples are very similar to lists, but they are immutable (i.e., unchangeable)

Tuples are written with round brackets as follows:

33 of 33

Tuples in Python

Like lists, tuples can:

Contain any and different types of elements

Contain duplicate elements (e.g., (1, 1, 2))

Be indexed exactly in the same way (i.e., using the [] brackets)

Be sliced exactly in the same way (i.e., using the [::] notation)

Be concatenated (e.g., t = (1, 2, 3) + (“a”, “b”, “c”))

Be repeated (e.g., t = (“a”, “b”) * 10)

Be nested (e.g., t = ((1, 2), (3, 4), ((“a”, “b”, ”c”), 3.4))

Be passed to a function, but will result in pass-by-value and not pass-by-reference outcome since it is immutable

Be iterated over