Class xi Python List

Python List

  • How to create a list?
  • How to access elements from a list?
    • List Index
    • Negative Indexing
  • How to slice lists in Python?
  • How to change or add elements to a list?
  • How to delete or remove elements from a list?
  • Python List Methods
  • List Comprehension
  • Other List Operations in Python
    • List Membership Test
    • Iterating Through a List
    • Built-in Functions with List

Python offers a range of compound data types often referred to as sequences. List is one of the most frequently used and very versatile data type used in Python.

How to create a 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.).

# empty list
n = []

# list of integers
n = [1, 2, 3]

# list with mixed datatypes
n = [1, “Hello”, 3.4]

Also, a list can even have another list as an item. This is called nested list.

# nested list
n = [“hello”, [8, 4, 6], [‘a’]]

How to access elements from a list?

There are various ways in which we can access the elements of a list.

List Index

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.

Note : Trying to access an element other then the range will raise an IndexError. The index must be an integer. We can’t use float or other types, this will result into TypeError.

Nested list are accessed using nested indexing.

Examples:

n1=[‘c’ , ‘o’ , ‘m’ , ‘p’ , ‘u’ , ‘t’ , ‘e’ , ‘r’]

0

1

2

3

4

5

6

7

c

o

m

p

u

t

e

r

print(n1)
#output : [‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
print(n1[0])
#output: c
print(n1[2])
#output : m
print(n1[4])
#output: u
#print(n1[2.5]) #error
#error

Example:

n2=[‘c’ , ‘a’ , ‘t’ , ‘a’ , ‘l’ , ‘y’ , ‘s’ , ‘t’]

0

1

2

3

4

5

6

7

c

a

t

a

l

y

s

t

n2=[‘c’ , ‘a’ , ‘t’ , ‘a’ , ‘l’ , ‘y’ , ‘s’ , ‘t’]

print(n2)
print(n2[0])
print(n2[2])
print(n2[5])
print(n2[7])
print(n2[1])
print(n2[1.7])

output:
[‘c’, ‘a’, ‘t’, ‘a’, ‘l’, ‘y’, ‘s’, ‘t’]
c
t
y
t
a

 

n1=['c','o','m','p','u','t','e','r']
print(n1)
print(n1[0])
print(n1[2])
print(n1[4])
Output

['c', 'o', 'm', 'p', 'u', 't', 'e', 'r']
c
m
u
>>> 
n2=['c','a','t','a','l','y','s','t']
print(n2)
print(n2[0])
print(n2[2])
print(n2[5])
print(n2[7])
print(n2[1])

#Output

['c', 'a', 't', 'a', 'l', 'y', 's', 't']
c
t
y
t
a
>>> 

Example of Nested List

n = [“Happy”, [2,0,1,5]]
# Nested indexing
print(n[0][0])
# Output: H
print(n[0][1])
# Output: a
print(n[1][0])
# Output: 2
print(n[1][1])
# Output: 0
print(n[1][3])
# Output: 5

Output:
H
a
2
0
5

Negative indexing

Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the second last item and so on.

Example:

Positive indexing (from left side index starts from 0(zero))

0

1

2

3

4

5

6

7

c

o

m

p

u

t

e

r

-8

-7

-6

-5

-4

-3

-2

-1

Negative indexing (from right side index starts from -1)

n1=[‘c’ , ‘o’ , ‘m’ , ‘p’ , ‘u’ , ‘t’ , ‘e’ , ‘r’]

print(n1)
print(n1[-1])
print(n1[-2])
print(n1[-4])

output:
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
r
e
u

 

Example:

n2=[‘c’ , ‘a’ , ‘t’ , ‘a’ , ‘l’ , ‘y’ , ‘s’ , ‘t’]

Positive indexing (from left side index starts from 0(zero))

0

1

2

3

4

5

6

7

c

a

t

a

l

y

s

t

-8

-7

-6

-5

-4

-3

-2

-1

Negative indexing (from right side index starts from -1)

n2=[‘c’ , ‘a’ , ‘t’ , ‘a’ , ‘l’ , ‘y’ , ‘s’ , ‘t’]

print(n2)
print(n2[-0]) # -0 is 0
print(n2[-2])
print(n2[-5])
print(n2[-7])
print(n2[-1])
print(n2[1])
print(n2[4])
print(n2[6])

output:
[‘c’, ‘a’, ‘t’, ‘a’, ‘l’, ‘y’, ‘s’, ‘t’]
c
s
a
a
t

How to slice lists in Python?

We can access a range of items in a list by using the slicing operator (colon).

print(n1[1:3])

#counting starts from 0

#will display elements from start to position -1

Example:

n1=[‘c’ , ‘o’ , ‘m’ , ‘p’ , ‘u’ , ‘t’ , ‘e’ , ‘r’]

0

1

2

3

4

5

6

7

c

o

m

P

u

t

e

R

n1=[‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]

print(n1)
output: [‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]

print(n1[1:3])
output: [‘o’, ‘m’]
print(n1[1:4])
output : [‘o’, ‘m’, ‘p’]
print(n1[1:5])
output: [‘o’, ‘m’, ‘p’, ‘u’]
print(n1[2:3])
output: [‘m’]
print(n1[2:4])
output: [‘m’, ‘p’]
print(n1[2:5])
output: [‘m’, ‘p’, ‘u’]

Number Of Visitors

0125447