1 of 26

2 of 26

List

  • In Python programming, a list is created by placing all the items (elements) inside a square bracket [ ], separated by commas.
  • It can have any number of items and they may be of different types (integer, float, string etc.).
  • A list is a collection of similar or different types of Data items.
  • Example:
  • numbers

  • A list of 10 natural numbers.
  • Numbers=[1,2,3,4,5,6,7,8,9,10]

1

2

3

4

5

6

7

8

9

10

0

1

2

3

4

5

6

7

8

9

3 of 26

Creation of a list :

  • A List is created by placing items inside []separated by commas.
  • Example:
  • An empty list
  • Numbers=[]

  • Example:
  • A list with duplicate items. Number=[1,2,2,3,3,3,4,4,4,4]

  • Numbers

1

2

2

3

3

3

4

4

4

4

0

1

2

3

4

5

6

7

8

9

4 of 26

  • A list is Different set of items;
  • Example:
  • Items=[1,2,’s’,’hai’,1.23456]
  • Items

1

2

s

hai

1.23456

0

1

2

3

4

5 of 26

  • Creation of a list
  • # empty list
  • mylist = []
  • # list of integers
  • mylist = [1, 2, 3]
  • # list with mixed datatypes
  • mylist = [1, "Hello", 3.4]
  • Also, a list can even have another list as an item. This is called nested list.
  • # nested list
  • mylist = ["Hello", [8, 4, 6], ['a']]
  • Features of List:
  • A List can have dublicate items
  • Items in a list are Mutable.
  • List can store items of various types.

6 of 26

Accessing Elements of List:

  • There are various ways in which we can access the elements of a list.
  • List Index Operator :
  • We can use the index operator [] to access an item in a list.
  • Index starts from 0. So, a list having 5 elements will have index from 0 to 4.
  • Trying to access an element other that this will raise an IndexError. The index must be an integer.
  • We can't use float or other types, this will result into TypeError.
  • List elements are placed one after other.
  • Eac element is allotted a unique number called index.

7 of 26

  • Example:
  • A List is contain multiple of 5 up to 20.
  • List1=[5,10,15,20]
  • List1

  • >>>List1[0]=5
  • >>>List1[3]=20
  • Example: mylist = ['h','e','l','l','o']
  • >>>print(my_list[0])
  • Output:
  • ‘h’
  • >>>print(mylist[2])
  • ‘l’

5

10

15

20

0

1

2

3

8 of 26

  • Nested List :
  • Nested List is a List containing anther list.
  • Nested list are accessed using nested indexing.
  • Example:nlist1=[[1,2,3],”VVIT”,[4,5,6]]

  • Nlist1

  • Nlist1[0][0]=1
  • Nlist1[2]=[4,5,6]
  • Example:
  • nlist2 = ["Happy", [2,0,1,5]]
  • # Nested indexing
  • >>>print(nlist[0][1])
  • >>>print(nlist[1][3])

“VVIT”

0

1

2

1

2

3

4

5

6

0

1

2

0

1

2

9 of 26

  • Negative indexing :
  • Python allows negative indexing for its sequences.
  • Accessing the element from the last.
  • The index of -1 refers to the last item, -2 to the second last item and so on.
  • Example:List1=[5,10,15,20]
  • List1

  • List1[-1]=20
  • List1[-4]=5
  • Example:
  • >>>mylist = ['h','e','l','l','o']
  • >>>print(mylist[-1])
  • >>>print(mylist[-5])

5

10

15

20

0

1

2

3

-4

-3

-2

-1

10 of 26

11 of 26

12 of 26

Python List Methods

append() - Add an element to the end of the list

extend() - Add all elements of a list to the another list

insert() - Insert an item at the defined index

remove() - Removes an item from the list

pop() - Removes and returns an element at the given index

clear() - Removes all items from the list

index() - Returns the index of the first matched item

count() - Returns the count of number of items passed as an argument

sort() - Sort items in a list in ascending order

reverse() - Reverse the order of items in the list

copy() - Returns a shallow copy of the list

13 of 26

�Change or add elements to a list (because list is ‘mutable’):

  • Element can be added to a list in different ways,following are the methods to add element to the list.

1.append()

2.insert()

3.extend()

  • A built in method used to add an item at the end of list.
  • Syntax:
  • list.append(value)

14 of 26

15 of 26

  • Duplicate append() method to add multiple items.

16 of 26

  • Insert(): A built in method used to add an item at specific position.
  • Syntax:
  • list.insert(position,value)
  • Example:

17 of 26

  • Extend():
  • A built in method used to add all items of one list in anther list.
  • Syntax:
  • list1.extend(list2)

18 of 26

  • >>>list = [2, 4, 6, 8]
  • # change the 1st item
  • >>>list[0] = 1
  • # Output: [1, 4, 6, 8]
  • print(list)
  • # change 2nd to 4th items
  • list[1:4] = [3, 5, 7]
  • # Output: [1, 3, 5, 7]

19 of 26

20 of 26

  • Example: Python Program to Swap the First and Last Value of a List

21 of 26

List Operators :

  1. Slicing Operator (:)
  2. Concatenation (+)
  3. repeat Operator (*)
  4. insert() Method

1. Slicing Operator (:)

  • We can access a range of items in a list by using the slicing operator (colon :).
  • Syntax:
  • listname[start index:end index:jumping index(optional)]
  • mylist = ['w','e',’l’,'c','o','m','e']
  • # elements 3rd to 5th
  • print(mylist[2:5])
  • # elements beginning to 4th
  • print(mylist[:-5])
  • # elements 6th to end
  • print(mylist[5:])
  • # elements beginning to end
  • print(mylist[:])

22 of 26

23 of 26

24 of 26

2. Concatenation (+)

  • The ‘+’ Operator is used to combine two lists. This is called concatenation.
  • Example:
  • list1=[1,3,5,7,9]
  • List2=[2,4,6,8,10]
  • Elements of List1 followed by elements in list2.
  • List1+list2

25 of 26

3. repeat Operator (*) :

  • The ‘*’ operator repeats a list for the given number of times.
  • Example:
  • list1=“VVIT”
  • Print(list1*5)

26 of 26

  • insert() Method
  • Furthermore, we can insert one item at a desired location by using the method insert() or insert multiple items by squeezing it into an empty slice of a list.