1 of 37

Python List

For 5th Semester Hons

By

Sumana Bandyopadhyay

2 of 37

  • Creating List
  • Access, Modify and Delete list Elements
  • Merge Lists
  • Use the slicing syntax to operate on sublists
  • Loop over lists

3 of 37

  • A list is a compound data type, you can group values together
  • A list contains items separated by commas and enclosed within square brackets ([]).
  • Lists are similar to arrays in C.
  • One difference between them is that the items belonging

to a list can be of different data type.

  • The values stored in a list can be accessed using the [ ] operator
  • Index starts at 0.

4 of 37

5 of 37

6 of 37

  • What is the result of this code? nums = [5, 4, 3, 2, 1] print(nums[-2])

7 of 37

>>> math = 45

>>> science = 23

>>> social = 28

>>> marksList = [math, science, social]

>>>

>>> print(marksList) [45, 23, 28]

8 of 37

  • Create a list, areas, that contains the area of the hallway (hall), kitchen (kit), living room (liv), bedroom (bed) and bathroom (bath), in this order. Use the predefined variables.
  • Print areas with the print() function.

# area variables (in square meters)

hall = 11.25

kit = 18.0

liv = 20.0

bed = 10.75

bath = 9.50

# Create list areas

# Print areas

9 of 37

SN

Function with Description

1

len(list)

Gives the total length of the list.

2

max(list)

Returns item from the list with max value.

3

min(list)

Returns item from the list with min value.

4.

sum(list)

Returns the sum of all elements of list

10 of 37

11 of 37

12 of 37

  • Two List can be combined using +

operator.

  • We cannot add a integer with list using +

13 of 37

  • Forming new lists with a repeating sequence using the multiplication operator:
  • Create a list of 5 elements with initial value as 0

14 of 37

Insertion

Append(ele)

Add element at end of List

Insert(index , ele)

Add element at given index

Extend( seq )

Appends all the elements of seq to list

Deletion

Pop(index)

Delete element based on Index

Remove(key)

Delete element based on key

Count(key)

Returns the number of occurrences of key

Index(key)

Returns the index of key element

Sort

Sorts elements of List

Reverse

Reverses elements of list

15 of 37

  • We can add elements to list.
  • To add one element, use append method.
  • This adds an item to the end of an existing list.

mylist = [] mylist.append(5) mylist.append(8) mylist.append(12)

#prints [5,8,12]

print(mylist)

16 of 37

17 of 37

insert(index, element)

18 of 37

  • Insert the elements in sorted order.

  • Algorithm
  • If list is empty, append element to list.
  • If element is less than first element, insert front.
  • Traverse till current element is less than

element to insert or till end of list

    • Insert the element.

19 of 37

  • We can add a series of elements using extend method or + operator.

20 of 37

  • a = [1,2,3]
  • b = [4,5,6]
  • Understand the difference between
  • a.append(b) and
  • a.extend(b)

21 of 37

  • To delete last element

22 of 37

  • To delete element at specific Index

23 of 37

  • Remove method takes the key element to delete and removes it if present.

24 of 37

25 of 37

26 of 37

27 of 37

28 of 37

29 of 37

  • In all the previous methods, we were not able to determine the index where the key element is present.

30 of 37

  • index() method finds the given element in a list and returns its position.
  • If the same element is present more than once, index() method returns its smallest/first position.
  • If not found, it raises

a ValueError exception indicating the element is not in the list.

31 of 37

  • The index method finds the first occurrence of a list item and returns its index.

If the item isn't in the list, it raises a ValueError.

  • letters = ['p', 'q', 'r', 's', 'p', 'u']
  • print(letters.index('r'))
  • print(letters.index('p'))
  • print(letters.index('z'))

2

0

ValueError: 'z' is not in list

32 of 37

33 of 37

34 of 37

  • The sort method can be used to sort the elements of list.

35 of 37

36 of 37

37 of 37