For Enquiry: 93450 45466

Length of List in Python



You will often need to group several elements in your program in order to process them as a single object.For this you will need to use different methods in collections.One of the most useful methods in collections module, in Python is a list.

Table Of Content

Lists In Python
Characteristics Of Lists
Methods In lists
Find the length of list using naive method
Find the length using len method
Find the length of list of lists
TIme COmplexity of running len method

Lists In Python

A list contains is a collection of multiple elements or items or arbitrary objects (in the programming sense).They are like arrays in the other languages but more versatile and flexible. 

Eg:

my_list=[1,‘fita’,’training’,[‘chennai’,’bangalore’],3]

Characteristics Of Lists

Lists are ordered i.e., their order is reserved after they are defined(unlike dictionaries or sets).
Lists are mutable i.e., their elements can be changed,or resized(unlike tuple or string).
Lists can contain elements with different data types with a finiteness.
Lists items can be concatenated
You can use logical operators with lists to test conditions.

Methods In Lists

Any element can be accessed using square brackets[] notation or .index(x) method.

{Note that the indexing starts at 0}

Elements can be appended to lists with the .append(x) , .insert(i,x) or .extend(iterable) method.
Elements can be removed from the lists with the .remove(x) or .pop() or .clear() element.
Elements can be changed by accessing and then assigning using = operator.
Elements can be concatenated and returned as a single object with .join() method.
A string can be converted to a list with the list() method.

There are many more useful methods in lists and you can grab insights of them, dynamically slicing lists and their powerful applications including real-time projects with live assistance, in Python Online Training by FITA.

Program 1: Finding the length of list using len method

len is a built-in method in python which returns the size of the object passed to it, irrespective of the index of last element.When a string is passed as an argument it returns the number of characters in the string.It is often used technique in programs these days.

my_list=[]

my_list.append(‘hello’,’readers’)

print(‘length of our list: ‘,len(my_list))

Output:

Length of our list: 3

Program 2: Finding the length of list with a custom method.

my_list=[0,1,1,2,3,5]

counter=0

for _ in my_list:

counter+=1

print(‘size of list with my method: ‘,counter)

 

Output:

size of list with my method: 6

Program 3: Find the length of the nested list

Nested lists have lists as an element in them. You can consider a nested list as a matrix..We can find the length of such lists, The length of the list can be using len method and the length of a sub list can be found by passing the sub list as argument to len method.

my_list=[[‘list 1’,‘element 1’,’element 2’],[‘list 2’, ‘element 1’, ‘element 2’]]


print(‘length of the list: ’,len(my_list)) 

print(‘length of the first sub list: ’,len(my_list[0]))

 

Output: 

length of the list: 2

length of the first sub list: 3

The other ways to find the length of the list includes using length_hint() and __len__ method. Now you can also write your own unique program to find the length of the nested list using for or while loops.

Time Complexity Of len function

len() function actually calls the __len__ method internally to return the size of the object and every object (list, tuple, set, dictionary, set, array.array) has its own __len__ method, so all it does is return self.length.

So the time complexity of the len() method is O(1) (in big-O notation) or constant time.

To get in-depth knowledge of Python along with its various applications and real-time projects, you can enroll in Python Training in Chennai or Python Training in Bangalore by FITA at an affordable price, which includes certification, support and career guidance assistance.






Quick Enquiry

Please wait while submission in progress...


Contact Us

Chennai, Bangalore & Online

  93450 45466

Coimbatore

 95978 88270

Tirupur

99401 22502

Madurai

97900 94102

Pondicherry

93635 21112

For Hiring

 93840 47472
 hr@fita.in

Corporate Training

 90036 23340


FITA Academy Branches

Chennai

FITA Academy - Velachery
Plot No 7, 2nd floor,
Vadivelan Nagar,
Velachery Main Road,
Velachery, Chennai - 600042
Tamil Nadu

    :   93450 45466

FITA Academy - Anna Nagar
No 14, Block No, 338, 2nd Ave,
Anna Nagar,
Chennai 600 040, Tamil Nadu
Next to Santhosh Super Market

    :   93450 45466

FITA Academy - T Nagar
05, 5th Floor, Challa Mall,
T Nagar,
Chennai 600 017, Tamil Nadu
Opposite to Pondy Bazaar Globus

    :   93450 45466

FITA Academy - Tambaram
Nehru Nagar, Kadaperi,
GST Road, West Tambaram,
Chennai 600 045, Tamil Nadu
Opposite to Saravana Jewellers Near MEPZ

    :   93450 45466

FITA Academy - Thoraipakkam
5/350, Old Mahabalipuram Road,
Okkiyam Thoraipakkam,
Chennai 600 097, Tamil Nadu
Next to Cognizant Thoraipakkam Office
& Opposite to Nilgris Supermarket

    :   93450 45466

FITA Academy - Porur
17, Trunk Rd,
Porur
Chennai 600116, Tamil Nadu
Above Maharashtra Bank

    :   93450 45466

FITA Academy - Pallikaranai
335A, 13th Main Rd,
Ram Nagar South Extn,
Pallikaranai, Chennai,
Tamil Nadu 600100

    :   93450 45466

Bangalore

FITA Academy Marathahalli
No 7, J J Complex,
ITPB Road, Aswath Nagar,
Marathahalli Post,
Bengaluru 560037

    :   93450 45466

Coimbatore

FITA Academy - Saravanampatty
First Floor, Promenade Tower,
171/2A, Sathy Road, Saravanampatty,
Coimbatore - 641035
Tamil Nadu

    :   95978 88270

FITA Academy - Singanallur
348/1, Kamaraj Road,
Varadharajapuram, Singanallur,
Coimbatore - 641015
Tamil Nadu

    :   95978 88270

Other Locations

FITA Academy - Madurai
No.2A, Sivanandha salai,
Arapalayam Cross Road,
Ponnagaram Colony,
Madurai - 625016, Tamil Nadu

    :   97900 94102

FITA Academy - Pondicherry
410, Villianur Main Rd,
Sithananda Nagar, Nellitope,
Puducherry - 605005
Near IG Square

    :   93635 21112

FITA Academy - Tiruppur
61D, Poongodi Towers 2nd floor,
Periyar Colony Bus Stop,
Tirupur - 641 652

    :   9940122502

Read More Read less
  • Are You Located in Any of these Areas

    Adyar, Adambakkam, Anna Salai, Ambattur, Ashok Nagar, Aminjikarai, Anna Nagar, Besant Nagar, Chromepet, Choolaimedu, Guindy, Egmore, K.K. Nagar, Kodambakkam, Koyambedu, Ekkattuthangal, Kilpauk, Meenambakkam, Medavakkam, Nandanam, Nungambakkam, Madipakkam, Teynampet, Nanganallur, Navalur, Mylapore, Pallavaram, Purasaiwakkam, OMR, Porur, Pallikaranai, Poonamallee, Perambur, Saidapet, Siruseri, St.Thomas Mount, Perungudi, T.Nagar, Sholinganallur, Triplicane, Thoraipakkam, Tambaram, Vadapalani, Valasaravakkam, Villivakkam, Thiruvanmiyur, West Mambalam, Velachery and Virugambakkam.

    FITA Velachery or T Nagar or Thoraipakkam OMR or Anna Nagar or Tambaram or Porur branch is just few kilometre away from your location. If you need the best training in Chennai, driving a couple of extra kilometres is worth it!