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.





  • Trending Courses

    JAVA Training In Chennai Software Testing Training In Chennai Selenium Training In Chennai Python Training in Chennai Data Science Course In Chennai Digital Marketing Course In Chennai DevOps Training In Chennai German Classes In Chennai Artificial Intelligence Course in Chennai AWS Training in Chennai UI UX Design course in Chennai Tally course in Chennai Full Stack Developer course in Chennai Salesforce Training in Chennai ReactJS Training in Chennai CCNA course in Chennai Ethical Hacking course in Chennai RPA Training In Chennai Cyber Security Course in Chennai IELTS Coaching in Chennai Graphic Design Courses in Chennai Spoken English Classes in Chennai Data Analytics Course in Chennai

    Spring Training in Chennai Struts Training in Chennai Web Designing Course In Chennai Android Training In Chennai AngularJS Training in Chennai Dot Net Training In Chennai C / C++ Training In Chennai Django Training in Chennai PHP Training In Chennai iOS Training In Chennai SEO Training In Chennai Oracle Training In Chennai Cloud Computing Training In Chennai Big Data Hadoop Training In Chennai UNIX Training In Chennai Core Java Training in Chennai Placement Training In Chennai Javascript Training in Chennai Hibernate Training in Chennai HTML5 Training in Chennai Photoshop Classes in Chennai Mobile Testing Training in Chennai QTP Training in Chennai LoadRunner Training in Chennai Drupal Training in Chennai Manual Testing Training in Chennai WordPress Training in Chennai SAS Training in Chennai Clinical SAS Training in Chennai Blue Prism Training in Chennai Machine Learning course in Chennai Microsoft Azure Training in Chennai Selenium with Python Training in Chennai UiPath Training in Chennai Microsoft Dynamics CRM Training in Chennai VMware Training in Chennai R Training in Chennai Automation Anywhere Training in Chennai GST Training in Chennai Spanish Classes in Chennai Japanese Classes in Chennai TOEFL Coaching in Chennai French Classes in Chennai Informatica Training in Chennai Informatica MDM Training in Chennai Big Data Analytics courses in Chennai Hadoop Admin Training in Chennai Blockchain Training in Chennai Ionic Training in Chennai IoT Training in Chennai Xamarin Training In Chennai Node JS Training In Chennai Content Writing Course in Chennai Advanced Excel Training In Chennai Corporate Training in Chennai Embedded Training In Chennai Linux Training In Chennai Oracle DBA Training In Chennai PEGA Training In Chennai Primavera Training In Chennai Tableau Training In Chennai Spark Training In Chennai Appium Training In Chennai Soft Skills Training In Chennai JMeter Training In Chennai Power BI Training In Chennai Social Media Marketing Courses In Chennai Talend Training in Chennai HR Courses in Chennai Google Cloud Training in Chennai SQL Training In Chennai CCNP Training in Chennai PMP Training in Chennai OET Coaching Centre in Chennai Business Analytics Course in Chennai NextJS Course in Chennai Vue JS Course in Chennai

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

    Adambakkam, Adyar, Akkarai, Alandur, Alapakkam, Alwarpet, Alwarthirunagar, Ambattur, Ambattur Industrial Estate, Aminjikarai, Anakaputhur, Anna Nagar, Anna Salai, Arumbakkam, Ashok Nagar, Avadi, Ayanavaram, Besant Nagar, Bharathi Nagar, Camp Road, Cenotaph Road, Central, Chetpet, Chintadripet, Chitlapakkam, Chengalpattu, Choolaimedu, Chromepet, CIT Nagar, ECR, Eechankaranai, Egattur, Egmore, Ekkatuthangal, Gerugambakkam, Gopalapuram, Guduvanchery, Guindy, Injambakkam, Irumbuliyur, Iyyappanthangal, Jafferkhanpet, Jalladianpet, Kanathur, Kanchipuram, Kandhanchavadi, Kandigai, Karapakkam, Kasturbai Nagar, Kattankulathur, Kattupakkam, Kazhipattur, Keelkattalai, Kelambakkam, Kilpauk, KK Nagar, Kodambakkam, Kolapakkam, Kolathur, Kottivakkam, Kotturpuram, Kovalam, Kovilambakkam, Kovilanchery, Koyambedu, Kumananchavadi, Kundrathur, Little Mount, Madambakkam, Madhavaram, Madipakkam, Maduravoyal, Mahabalipuram, Mambakkam, Manapakkam, Mandaveli, Mangadu, Mannivakkam, Maraimalai Nagar, Medavakkam, Meenambakkam, Mogappair, Moolakadai, Moulivakkam, Mount Road, MRC Nagar, Mudichur, Mugalivakkam, Muttukadu, Mylapore, Nandambakkam, Nandanam, Nanganallur, Nanmangalam, Narayanapuram, Navalur, Neelankarai, Nesapakkam, Nolambur, Nungambakkam, OMR, Oragadam, Ottiyambakkam, Padappai, Padi, Padur, Palavakkam, Pallavan Salai, Pallavaram, Pallikaranai, Pammal, Parangimalai, Paruthipattu, Pazhavanthangal, Perambur, Perumbakkam, Perungudi, Polichalur, Pondy Bazaar, Ponmar, Poonamallee, Porur, Pudupakkam, Pudupet, Purasaiwakkam, Puzhuthivakkam, RA Puram, Rajakilpakkam, Ramapuram, Red Hills, Royapettah, Saidapet, Saidapet East, Saligramam, Sanatorium, Santhome, Santhosapuram, Selaiyur, Sembakkam, Semmanjeri, Shenoy Nagar, Sholinganallur, Singaperumal Koil, Siruseri, Sithalapakkam, Srinivasa Nagar, St Thomas Mount, T Nagar, Tambaram, Tambaram East, Taramani, Teynampet, Thalambur, Thirumangalam, Thirumazhisai, Thiruneermalai, Thiruvallur, Thiruvanmiyur, Thiruverkadu, Thiruvottiyur, Thoraipakkam, Thousand Light, Tidel Park, Tiruvallur, Triplicane, TTK Road, Ullagaram, Urapakkam, Uthandi, Vadapalani, Vadapalani East, Valasaravakkam, Vallalar Nagar, Valluvar Kottam, Vanagaram, Vandalur, Vasanta Nagar, Velachery, Vengaivasal, Vepery, Vettuvankeni, Vijaya Nagar, Villivakkam, Virugambakkam, West Mambalam, West Saidapet

    FITA Velachery or T Nagar or Thoraipakkam OMR or Anna Nagar or Tambaram or Porur or Pallikaranai 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!