Let’s check out some exercises that will help understand len() function better.

Exercise 10-a

Using len() function find out how many items are in the list.


You can pass your object’s name inside len() function:
len(object)

 

answer_1 = len(lst)

Exercise 10-b

len() function can also tell the length of a string.


Find out the length of the string given below.


len(object)

msg_length = len(msg)

Exercise 10-c

How many keys are there in the dictionary?


len(object)

ans_1 = len(dict)

Exercise 10-d

Call the last element of the list by using the len() function.


len() function will give you the length of a list. Since index starts from 0, you can subtract 1 from the length to find the index of the last item. 

index_no = len(gift_list)-1

ans_1 = gift_list[index_no]

Exercise 10-e

Below is a nested data. Find out the length of the item: 'raincoat'. You can use reverse indexing.


[-1] will let you access the last item of a list.

When you have nested data, to reach the last element of the last element:
You can chain your accessing notation as: [-1][-1]

ans_1 = len(gift_list[-1][-1])