top of page

Lists

Lists are a mutable data type and we use square brackets to create a list.

Create a list

a = ['Red', 'Green', 'Blue']

print(a)

List can contain duplicate values.

a = ['Red', 'Green', 'Blue','Red','Yellow']

print(a)

List are ordered and can be access using the index number.

a = ['Red', 'Green', 'Blue','Red','Yellow']

print(a[1])

Change List Elements

With the help of index numbers we can change the list elements.

Change green to black.

a = ['Red', 'Green', 'Blue']

a[1] = 'black'

print(a)

Change green and blue to pink and purple.

a = ['Red', 'Green', 'Blue', 'black', 'Yellow']

a[1:3] = ['Pink','Purple']

print(a)

Replace green and blue to pink.

a = ['Red', 'Green', 'Blue', 'black', 'Yellow']

a[1:3] = ['Pink']

print(a)

Replace green to pink and purple.

a = ['Red', 'Green', 'Blue', 'black', 'Yellow']

a[1:2] = ['Pink', 'Purple']

print(a)

Add List Elements

With the help of append, insert and extend we can add elements to lists.

append()

Append helps us to add the element to the end of our list.

Add black at the end.

a = ['Red', 'Green', 'Blue']

a.append('Black')

print(a)

insert(pos, val)

Insert helps us to add an element in between our list.

pos => takes the position number where we want to add the new element.

val => takes the value to be inserted.

Insert Pink after green.

a = ['Red', 'Green', 'Blue']

a.insert(2,'Pink')

print(a)

extend()

extend helps us to add one list or other iterable object to other list.

add both the lists.

a = ['Red', 'Green', 'Blue']

b = ['Pink', 'Purple', 'Black']

a.extend(b)

print(a)

Delete List Elements

With the help of below methods we can delete elements of a list.

pop()

pop will delete the last element from the list if no index number is passed.

Delete the last element

a = ['Red', 'Green', 'Blue']

a.pop()

print(a)

pop(1)

We can delete the element of particular position with the help of pop by passing the index number.

Delete the second element

a = ['Red', 'Green', 'Blue']

a.pop(1)

print(a)

remove()

  • We can delete the element with the help of remove by passing the value.

  • If the value specified is not present then it will throw an error.

Delete Green

a = ['Red', 'Green', 'Blue']

a.remove('Green')

print(a)

del a[start:stop]

Delete can help us to delete more than one element by passing the slicing value.

Delete more than one element

a = ['Red', 'Green', 'Blue', 'Black']

del a[1:3]

print(a)

del a

Complete list can be deleted if we just write the varaible name without any index number

Delete more than one element

a = ['Red', 'Green', 'Blue', 'Black']

del a

print(a)

clear()

Clear method help us to clear all the elements from the list. But variable is not deleted.

Clear the List

a = ['Red', 'Green', 'Blue', 'Black']

a.clear()

print(a)

List Sort

List can be sort in ascending and descending order by using the sort method.

Ascending Sort

a = ['Red', 'Green', 'Blue']

a.sort()

print(a)

Descending Sort

a = ['Red', 'Green', 'Blue', 'Pink']

a.sort(reverse = True)

print(a)

List Copy

Copy a list to another variable

a = ['Red', 'Green', 'Blue']

b = a.copy()

print(b)

List Loop

Iterate over a list.

a = ['Red', 'Green', 'Blue']

for i in a:

    print(i)

List Reverse

Reverse a List.

a = ['Red', 'Green', 'Blue']

a.reverse()

print(a)

count()

Count number of time green occurs.

a = ['Red', 'Green', 'Blue', 'Green', 'Black']

b = a.count('Green')

print(b)

index()

Index number of first occurence of Green.

a = ['Red', 'Green', 'Blue', 'Green', 'Black']

b = a.index('Green')

print(b)

List Functions

Length of List

a = ['Red', 'Green', 'Blue', 'Green', 'Black']

print(len(a))

Maximum element in a List

a = [45,65,12,97,10]

print(max(a))

Minimum element in a List

a = [45,65,12,97,10]

print(min(a))

Sum of a list

a = [45,65,12,97,10]

print(sum(a))

List Comprehension

Suppose we want to create a list with numbers 1 to 10

Without list comprehension

a = []

for i in range(1,11):

    a.append(i)

print(a)

With list comprehension

a = [i for i in range(1,11)]

print(a)

  • As we can see that we write the above code in single line.

  • List comprehension is faster compared to traditional way.

bottom of page