1 of 28

LIST INTRODUCTION

2 of 28

What is List ?

  • It is Mutable datatype.
  • It is a collection of python datatype which is enclosed by square bracket [].and each element is separated by comma (,).
  • For ex: x=[1,3,4,”hello”]

3 of 28

Introducing CRUD Operation

4 of 28

How to create list?

A list can be created by passing n number of elements within [] and each element separated by comma.

For ex; x = [1,2,3,4,”hii”,27]

5 of 28

Ways to Read Data from List

list

6 of 28

Indexing in python

  • It refers to the position of an element in a list. It can be positive indexing & negative indexing.
  • Positive Index start with 0 ,1,2 .. and so on and 0 denote the first element
  • Negative Index start with -1,-2,-3 and so on and -1 denote the last element

7 of 28

Slicing in python

  • It used to access multiple elements from the list.
  • Syntax : [start : stop : step]

8 of 28

Ways to update the List Elements

9 of 28

Update - indexing

  • Update a list through indexing involve directly accessing an element at the specific position and modifying its value.
  • for ex: x = [1,2,34,5,6]
  • x[3]=13
  • x = [1,2,34,13,6]

10 of 28

Update - slicing

  • Update list through slicing involves specifying a range of indices to select a portion of the list. Then we can assign new values to this slice to update the list.
  • Syntax for slicing: list[start:stop:step]
  • Where , start = The index where slice begins.
  • stop = The index where slice stop.
  • step = The step size or interval between elements in the slice

my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

my_list[2:5] # Output: [2, 3, 4]

my_list[1:8:2] # Output: [1, 3, 5, 7]

11 of 28

List Methods

12 of 28

APPEND

  • Adds a single element to the end of the list.

Syntax: a.append(b)

13 of 28

EXTEND

Adds all elements of an iterable (e.g., list) to the end of the list.

Syntax: a.extend(iterable)

14 of 28

INSERT

Inserts an element at a specified position in the list.

Syntax: x.insert(index, element)

15 of 28

How to delete list?

16 of 28

Delete - indexing

  • Delete an item from a list by directly accessing its position and modifying the list accordingly.

  • x = [10, 20, 30, 40, 50]
  • del x[2]
  • print(x)
  • Output will be [10, 20, 40, 50]

17 of 28

Delete - Slicing

  • "Remove an item from a list by using slicing to directly access its position and modify the list accordingly.

  • x = [10, 20, 30, 40, 50]
  • del x[2:4]
  • print(x)
  • Output will be [10, 20 50]

18 of 28

How to delete list?

19 of 28

Pop()

  • Pop: The pop operation removes and returns the last element from a list,

  • For example:
  • X = [10, 20, 30, 40, 50]
  • X.pop()
  • Print(X)
  • Output: X = [10, 20, 30, 40]

20 of 28

remove()

  • Remove: The remove method deletes the first occurrence of a specified value from a list, not affecting other elements.

  • For example:
  • X = [10, 20, 30, 40, 50]
  • x.remove(20)
  • Print(x)
  • Output: x= [10, 30, 40]

21 of 28

clear()

  • Clear: The clear method removes all elements from a list

  • For example:
  • x = [10, 20, 30, 40, 50]
  • x.clear()
  • print(x)

22 of 28

del()

  • Del: The del statement removes an element at a specified index or the entire list from memory.

  • For example:
  • x = [10, 20, 30, 40, 50]
  • del x[2]
  • print(x)

23 of 28

List method

24 of 28

index()

  • index(element[, start[, end]]): Returns the index of the first occurrence of an element

  • For example:
  • x = [1, 2, 3]
  • x.index(2)
  • print(x)
  • Output: 1

25 of 28

count()

count(element): Returns the number of occurrences of an element.

For Example:

x = [1, 2, 2, 3]

x.count(2)

Print(x)

Output: 2

26 of 28

sort()

sort(key=None, reverse=False): Sorts the elements of the list in ascending order by default

For example:

x = [3, 1, 2]

x.sort()

Output: [1, 2, 3]

x.sort(reverse=True)

Output: [3, 2, 1]

27 of 28

reverse()

reverse(): Reverses the elements of the list in place.

For example:

x = [1, 2, 3]

x.reverse()

Output: [3, 2, 1]

28 of 28

copy()

copy(): Returns a shallow copy of the list.

For example:

x = [1, 2, 3]

y = x.copy()

Output: [1, 2, 3]