Computer Science, asked by sumayah23, 5 months ago

A list list1 is given. Write the code to print the length of the list​

Answers

Answered by tigremdostty
0

Answer:

Method 1 : Naive Method

In this method one just runs a loop and increases the counter till the last element of the list to know its count. This is the most basic strategy that can be possibly employed in the absence of other present techniques.

Code #1 : Demonstrating finding length of list using Naive Method

filter_none

edit

play_arrow

brightness_4

# Python code to demonstrate  

# length of list  

# using naive method  

 

# Initializing list  

test_list = [ 1, 4, 5, 7, 8 ]  

 

# Printing test_list  

print ("The list is : " + str(test_list))  

 

# Finding length of list  

# using loop  

# Initializing counter  

counter = 0

for i in test_list:  

     

   # incrementing counter  

   counter = counter + 1

 

# Printing length of list  

print ("Length of list using naive method is : " + str(counter))  

Output :

The list is : [1, 4, 5, 7, 8]

Length of list using naive method is : 5

Method 2 : Using len()

The len() method offers the most used and easy way to find length of any list. This is the most conventional technique adopted by all the programmers today.

filter_none

edit

play_arrow

brightness_4

# Python program to demonstrate working  

# of len()  

a = []  

a.append("Hello")  

a.append("Geeks")  

a.append("For")  

a.append("Geeks")  

print("The length of list is: ", len(a))  

Output:

The length of list is:  4

filter_none

edit

play_arrow

brightness_4

# Python program to demonstrate working  

# of len()  

n = len([10, 20, 30])  

print("The length of list is: ", n)  

Output:

The length of list is:  3

Method 3 : Using length_hint()

This technique is lesser known technique of finding list length. This particular method is defined in operator class and it can also tell the no. of elements present in the list.

Code #2 : Demonstrating finding length of list using len() and length_hint()

filter_none

edit

play_arrow

brightness_4

# Python code to demonstrate  

# length of list  

# using len() and length_hint  

from operator import length_hint  

 

# Initializing list  

test_list = [ 1, 4, 5, 7, 8 ]  

 

# Printing test_list  

print ("The list is : " + str(test_list))  

 

# Finding length of list  

# using len()  

list_len = len(test_list)  

 

# Finding length of list  

# using length_hint()  

list_len_hint = length_hint(test_list)  

 

# Printing length of list  

print ("Length of list using len() is : " + str(list_len))  

print ("Length of list using length_hint() is : " + str(list_len_hint))  

Output :

The list is : [1, 4, 5, 7, 8]

Length of list using len() is : 5

Length of list using length_hint() is : 5

Similar questions