Python List Length | How to Length of a List in Python


Python List Length – Table of Content

What is a list in python?

A list is a data structure that contains an ordered sequence of elements.It is a mutable or changeable object. The values in the list are called items.The values can be of different types.We can gather related set of values together that belong to a group and store them in a list. It enables us to perform any operation on all the data present in a list at once.The items should be enclosed in square brackets and separated by a comma (,). We can create a list with a combination of numbers and string characters. A list can be defined in the following ways.

ListA = [1, 2, 3, 4];

ListB = ['Hi', 'Hello'];

ListC = ["Hi", "Hello"];

   Become a python Certified professional  by learning this HKR Python Training !

Find the length of a list

The length of a list in python can be found through two methods. 

Len() Method

It is a built-in function provided by python for getting the total number of items in a list, array, dictionary, etc. It takes the name of the list as an argument and returns the length of the given list. The len() method is the most commonly used method in python for finding the length of a list. Here is the syntax of the length method.

len()

is the name of the list 

Let’s look at an example program.

ListA = [1, 2, 3, 4, "Hello", "World"]

print ("Number of items in the list = ", len(ListA))

The output will be as follows. 

Number of items in the list =  6

We can also give the list directly as an argument to the len() method.

Length = len([1, 2, 3, 4, "Hello", "World"]);
print("Number of items in the list = ", Length);

The output will be as follows. 

Number of items in the list =  6

Length_hint() method

This method is not much known to programmers. It takes in a list or list name as an input argument and returns the number of items in the list. Here is a sample program that shows the use of the length_hint() method.

from operator import length_hint 

ListA = [1, 2, 3, 4, "Hello", "World"]

print ("Number of items in the list = ", length_hint(ListA))

The output for the above program is as follows.

Number of items in the list =  6

Naive Method

Apart from the len() method, there is another way to find the length of a list in Python. The naive approach is one of the most common methods when there is no alternative. It allows you to run the loop and count the items. Let us see its implementation :-

ListA = [1, 2, 3, 4, "Hello", "World"];

print("Input List = ", str(ListA));

count = 0;

for i in ListA:

count = count + 1

print("Number of items in the list = ", count);

The output for the above program will be as follows.

Input List =  [1, 2, 3, 4, 'Hello', 'World']

Number of items in the list =  6

This is the most basic method for finding the length of a list. But the len() method is always preferable to use.

Acquire Keras certification by enrolling in the HKR Keras Training program in Hyderabad!

Python Training Certification

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

Performance analysis of the three methods

Before choosing a method, we can do performance analysis and choose based on that. Let’s take a sample program that calculates the time taken to run each method. 

from operator import length_hint 

import time 

ListA = [1, 2, 3, 4, "Hello", "World"];

print("Input List = ", str(ListA));

#Navie method

naive_start = time.time() 

count = 0

for i in ListA: 

    count = count + 1

naive_end = str(time.time() - naive_start) 

print ("Time taken using naive method = ", naive_end) 

# len() method

len_start = time.time() 

list_len = len(ListA) 

len_end = str(time.time() - len_start) 

print ("Time taken using len() method = ", len_end)

# length_hint() method

hint_start = time.time() 

list_len_hint = length_hint(ListA) 

hint_end = str(time.time() - hint_start) 

print ("Time taken using length_hint() method = ", hint_end)

 

The output for the above program will be as follows.

Input List =  [1, 2, 3, 4, 'Hello', 'World']

Time taken using naive method =  4.76837158203125e-07

Time taken using len() method =  1.6689300537109375e-06

Time taken using length_hint() method =  9.5367431640625e-07

We can see that the len() method has taken less time than the other methods.

Big Data Analytics, python-list-length-description-0, Big Data Analytics, python-list-length-description-1

Subscribe to our YouTube channel to get new updates..!

Using length method in a loop

The len() method not only returns the length of the list, but it can also return the length of each string in the list. We can loop through the list, apply the len() method on them to get the number of characters of an item specified within the list. We can only apply it on the string items given in the list. Let’s see how we can do that.

ListA = ["Hello", "World", "Welcome", 3, 4]

print ("Number of items in the list = ", len(ListA))

for i in ListA:

print("Length of item = ", len(i))

   Top 50 frequently asked Python interview Question and answers !

The output for the above program will be as follows. 

Number of items in the list =  5

Length of item =  5

Length of item =  5

Length of item =  7

As you can see here, it only returned the length of only string items.

Checking whether a list is empty or not

We can check if a list is empty or not in our python program using the len() method. Based on the return value of the len() method, we can specify a set of actions to be performed. Let’s take a look at a sample program of it. 

ListA = ["Hello", "World", "Welcome", 3, 4]

ListB = []

print ("Number of items in ListA = ", len(ListA))

print ("Number of items in ListB = ", len(ListB))

if len(ListA) == 0:

print("The ListA is empty")

else:

print("The ListA is not empty")

if len(ListB) == 0:

print("The ListB is empty")

else:

print("The ListB is not empty")

The output for the above program will be as follows.

Number of items in ListA =  5

Number of items in ListB =  0

The ListA is not empty

The ListB is empty

Python Training Certification

Weekday / Weekend Batches

Conclusion

The len() is the method used by all the programmers today.Now that you know the utility of the len() method, you can use it in various ways, as explained in this post. Start experimenting with finding the length of a list in your code. For more information on python,refer to the python tutorials and blogs on our website.

Related Articles:

1. Python Partial Functions

2. Python Split Method

3. Python Generators

4. Enumerate ( ) in python 

5.Python for DevOps



Source link

Leave a Reply

Subscribe to Our Newsletter

Get our latest articles delivered straight to your inbox. No spam, we promise.

Recent Reviews


What Is TOGAF?

TOGAF is the abbreviation for The Open Group Architecture Framework. This framework while staying on the budget enables the businesses to plan, design, build and implement the infrastructure with minimal errors. In 1995, TOGAF was developed by Open Group. It was offered to the organizations to use it internally for free. It cannot be used by the organizations for commercial purposes. For using TOGAF, Open Group Consortium also offers certified courses and tools which can be used and implemented by the businesses.

As a result of constant contributions from various experts in TOGAF, a method called ADM(Architecture Development Method) was developed. ADM enables the architects to build an enterprise that satisfies the needs of various systems and organizations. In order to attain this the organizations must work with the trained as well as certified professionals who can make a seamless design process. A strategy will be designed and implemented by them that will best suit the requirements of the organization.

How Does TOGAF Work?

Working of TOGAF is in accordance with three elements that are used for Enterprise Architecture Development.

The first element is usage of precise and recognized terminology in the process of development. An organization will invariably experience confusion and delays if its experts are not given a clear understanding of both what they are saying and how they are saying it. In a clear collaborative environment, practitioners can learn and apply the TOGAF enterprise architecture concepts. Additionally, this guarantees enhanced vision clarity for all parties involved, including business and IT experts pursuing specific objectives. Even certified professionals can benefit from using TOGAF terminology to connect with others in various businesses and sectors. 

The second element is a collection of precisely specified processes by the Architecture Development Method of TOGAF. The TOGAF ADM is a cycle for developing enterprise architecture that specifies certain crucial elements for experts to adhere to. It leads users through a transparent and adaptable technique, assisting them in putting their knowledge to practice and growing it. The ADM begins by assisting practitioners in creating a precise description of the existing architecture they have. The exact composition of the framework they wish to build is then clarified with its assistance. In order to close this gap, by outlining, it concludes four crucial factors to concentrate on:

  1. Data architecture (along with the physical and logical data components)
  2. Application Architecture
  3. Technology Architecture
  4. Business Architecture

These fields are also always changing; for example, TOGAF Data Architecture and Business Architecture are currently garnering a lot of attention on their own. Organizations can establish comprehensive development roadmaps using this technique. Instead of taking a rigid, “one size fits all” approach, it achieves this by giving them a base upon which to build. Even yet, the framework is still adaptable enough to include other frameworks like COBIT and ITIL.

The standard gives all the resources required to carry out architectural development, which brings us to the final component. The framework, for instance, lays out a precise process for managing and documenting modifications as they happen. The time needed for future ADM cycles can also be reduced by reusing TOGAF reference architectures and artifacts.

TOGAF History and Facts

  • In 1993, TOGAF was developed as a Technical Architecture approach. 
  • In 1995 it was expanded as a complete Enterprise Architecture framework called TAFIM(Technical Architecture Framework for Information Management by the Open Group Consortium.
  • In December 2001, TOGAF 7 was released as a “Technical Edition”.
  • A Year later, TOGAF 8 was released as an Enterprise Edition.
  • In December 2003, TOGAF 8.1 was revised and reprinted
  • In 2005, TOGAF®TM was registered formally as a trademark of Open Group. 
  • In November 2006, again TOGAF was updated as TOGAF® 8.1.1
  • In December 2011, TOGAF® 9.1 was released. It was the latest version upgrade. It is one of the latest frameworks used by 80 percent of the Enterprise Architects. TOGAF® 9.1 has become the most successful framework till now. As evidenced by its latest expansion in the Enterprise Architecture, Of 22,900 TOGAF certified, 16,000 are ToGAF 9.1 qualified.

From the beginning of 1993, The Open Group is the proprietor and administering the groups which determines the professional path for TOGAF.

What is Enterprise Architecture?

Enterprise Architecture is the conceptual outline which determines the organization’s structure and operation. The primary objective of Enterprise Architecture is to find out the way an organization can accomplish its present and future goals effectively. The Enterprise Architecture includes a practice of planning, designing, analyzing and implementing the analysis on the enterprise. 

Enterprise Architecture focuses on putting old applications and processes together to make an effort to form a flawless environment, which is helpful to organizations going through digital transformation. In the 1980s, as the business technologies advanced and the need for the adoption of technological change became crucial to business strategy, EA framework usage increased. Later this method was extended to include not just the information technology but the entire business. In this way the alignment of the rest of the Organizations with the digital transformation will be ensured.

Enterprise Architecture Concept varies for different organizations. So for two different organizations it won’t be the same. Even different departments of the organization may interpret EA differently. For example, the programmers and other IT professionals consider enterprise architecture strategies with regards to infrastructure. Programmers and other IT experts, for instance, view enterprise architecture initiatives in terms of the management, application, and infrastructure components they have authority over. The implementation of business structure analysis is still up to enterprise architects, though.

What is the TOGAF Architecture Framework?

TOGAF is an open architecture framework and also known as “enterprise architecture methodology”. The TOGAF architecture framework offers a high-level enterprise framework for various software development. The main purpose of using the TOGAF architecture framework is to organize the multiple development processes by using a systematic approach and with the help of this approach we can reduce the errors, helps to maintain the timeline, budget management, and aligning the multiple information technology services to produce the high-quality products.

The first TOGAF was developed in 1995, and in 2016, it reached the peak, this is because almost 80% of the companies and also fortune 500 companies use this TOGAF framework due to its high-level enterprise benefits. TOGAF is a free software to use internally, but not available for commercial purposes. As we know that each software tool accelerated with various certifications. So in the case of TOGAF, we can see more than eight industry-oriented certificates and you will get 71 accelerated courses related to TOGAF.

TOGAF Architecture Framework Overview:

As I said earlier, TOGAF offers various benefits like reducing errors, aligning IT goals, and maintaining the budget. With the help of the TOGAF architecture framework, it’s very easy to define and organize the software requirements before starting your project, and you can also analyze the errors quickly. TOGAF is a kind of software tool for assisting the acceptance, use, maintenance, and production of software products. The TOGAF can also be considered as a method- this method is known as the “TOGAF architecture development method”. This type of method mainly used to develop enterprise architecture that addresses the business needs. 

TOGAF Framework:

The TOGAF framework is mainly used to perform high-level industry tasks. As we discussed earlier, there are a lot of benefits offered by TOGAF architecture. Now it’s time to know about the TOGAF framework,

TOGAF framework

Join our Togaf Certification Program Training today and enhance your skills to new heights!

Cyber Security Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

TOGAF framework is a widely used architecture framework, and this offers various approaches for designing, implementing, planning, and governing enterprise-level information technology architecture. Usually, TOGAF architecture is modeled in four levels such as business, data, applications, and technology. This TOGAF relay on processes like modularization, standardization, and proven products, services. TOGAF is designed on TAFIM or a technical architecture framework for information management. The architecture development method or ADM is the heart of TOGAF. This ADM method enables businesses to establish a process around the software development life cycle and also customize the specific organization’s needs.

TOGAF Pillars

The following are the three main pillars of TOGAF architecture:

1. Enterprise Architecture Domain

As we already know that TOGAF is based on various interrelated areas of the specification known as “Architecture domains”.

Below are the key points;

1. Business architecture defines the business strategy, organization, key business process, and governance of the organization.

2. Data architecture describes the structure of the organization to define logical and physical data assets and multiple data-associated data management resources.

3. Application architecture offers a blueprint for the individual system to be deployed, establishes the interaction between the application systems, and also their relationships to the core business process to organize them with the framework architecture for services will be exposed to integrate the business functions.

4. Technical architecture describes the hardware, networking, and software infrastructure to support the deployment of the mission-critical business applications.

2. Architecture Development Method:

The architecture development method or ADM used to define an enterprise architecture that will meet the business and IT infrastructure. This also helps to tailored to the organization’s needs and manages the execution of architecture planning activities. This process is iterative and cyclic. Each iteration step checks with requirements and involves combining both data architecture and application resources. This method also offers entire information architecture. Here the performance training can also be applied to the requirement phase, business architecture, information system architecture, and technology architecture. One more point to remember is that information system architecture can also be applied with both data architecture and application architecture.

3. Enterprise Continuum Architecture:

The enterprise continuum is a way to classify the solutions and architecture that helps to form the foundation architectures to organize specific architecture repositories. This also consists of architectural models, architecture descriptions, artifacts, and architecture patterns. This enterprise continuum consists of both architecture continuum and solution continuum. This architecture continuum describes the reusable structures that also rules, relationships, and representation. The solution continuum describes the implementation of the architecture continuum to define the solution building blocks.

Top 50 Most frequently asked TOGAF Interview Questions!

Programming & Frameworks, what-is-togaf-description-1, Programming & Frameworks, what-is-togaf-description-2

Subscribe to our YouTube channel to get new updates..!

Features of TOGAF 9.1

The following are the important features of the latest version of TOGAF 9.1:

a. Continent Metamodel Component: This TOGAF 9.1 version lifecycle process the creation and management of business enterprise architecture to support ADM methods. This also helps to implement the business proprietary architecture using tools, output checklist, richer, and streamlined approach.

b. Partitioning Feature: TOGAF latest version 9.1 that offers suggestions and guidance to participate in the specific architecture within the business enterprise.

c. Architecture Repository: This feature acts as a document that consists of participants, details, enterprise architecture, and relevant projects. This also includes the ideas, policies, framework process, and designs.

d. Enterprise Continuum Feature: this is a kind of documents that addresses the abstract concepts and also defines the framework architecture repository. The enterprise continuum feature helps IT and other business units which are related to one another.

e. ADM Guidelines and Techniques: this feature expands the ADM information that includes how to apply the guidelines within your organizations. This also helps to establish security and protective considerations.

Benefits of using TOGAF

TOGAF has the following benefits:

  • Rather than just having a rigid structure, ADM (architectural development model) can be changed to match the business needs.
  • It can be greatly scaled up to decrease errors.
  • It is possible to duplicate development procedures with few mistakes and flaws
  • Researchers suggest that qualified TOGAF experts can expect to make an appealing annual income of between $10,000 and $20,000.

TOGAF Tools and Certifications:

In this section, we are going to explain the various TOGAF tools used and certifications offered to master the TOGAF framework architecture;

1. Alfabet AG software: the versions included are planning IT 7.1 and later.

2. Avolution software: This version includes ABACUS 4.0 and later.

3. Bizz Design software:  this version supports Bizz design Enterprise studio.

4. BOC groups: The software includes ADOIT.

5. Orbus software: Iserver business and IT transformation suite 2015 and later.

6. Planview software: the software includes Troux.

7. Software AG: this software includes ARIS version 9.0 and later.

8. Sparx systems: this software includes the Enterprise Architect V12.

The certifications included are:

1. Udemy foundation enterprise certification training.

2. Edureka TOGAF certified training.

3. Good elearning TOGAF 9 certified training.

4. Global knowledge TOGAF 9.1 certification training for practitioners’ level 1 and level 2.

5. E-learning Launchpad TOGAF 9.1 certification training.

6. EA principals TOGAF 9 course: this includes level 1 and level 2 combined.

7. QA TOGAF 9.1 certification course.

Cyber Security Training

Weekday / Weekend Batches

Advantages of TOGAF Architecture:

The following are the few advantages of TOGAF architecture;

1. This TOGAF architecture framework gives us a useful language for communicating and sharing ideas about how the IT system can/ should support business needs.

2. TOGAF architecture also provides a process to assist the development of enterprise architecture and ensures all aspects are addressed in your business enterprises.

3. The various methodologies like TOGAF ADM offer templates for enterprise architecture development work.

4. Facilitate collaboration and communication and describing the process of enterprise architecture.

5. Develops the information system architecture, develops a baseline and targets the architecture, and analyzes the gaps.

6. Prepare the organization for a successful architecture project.

How Can a TOGAF Certification Help ?

In today’s competitive business environment, staying ahead of your competitors in any field is necessary. There are numerous things you can really do to maintain an upper hand when it comes to enterprise architects. One strategy you might use to get forward is to get your TOGAF certification. Enterprise architects of the highest caliber are required in every area out there. You need to possess this certification in order to notify recruiters of your effectiveness and capacity. Today, it serves as a sign that demonstrates your value to potential employers and clients.

One of the Best Ways to Begin a Career in Enterprise Architecture is to Obtain a TOGAF Certification. It appears to be recognized everywhere in the world and doesn’t cost much. Possessing a TOGAF Certification will advance your career in the right direction, to the next stage, and finally to the greatest heights. You will start receiving better opportunities once you complete this qualification. It enables you to design and carry out architecture plans, meetings, and workshops in a more effective way.

Conclusion:

In this TOGAF architecture framework blog, we have explained about overview, benefits, architecture, advantages, and limitations of TOGAF. In this blog, we have also mentioned the major certifications training offered by various top most institutions. As per Gartner’s report, almost 80 percent of major companies and 60 percent of the fortune 500 companies adopt the TOGAF framework in their businesses. The major key roles of TOGAF included are reducing errors while developing software, budget management, and aligning IT components. So learning TOGAF will be more beneficial for those who want to pursue their career as a framework architect or software architect.

Related Article:

What is CISA Certification



Source link