banner



how to find length of list in python

Python has a built-in functionlen() for getting the total number of items in a listing, tuple, arrays, dictionary etc.

The len() method takes an statement where yous may provide a list and it returns the length of the given list.

Few Examples and Related Topics

  • An example of list length
  • Array length case
  • A dictionary length instance
  • Python Listing tutorial
  • The Len() function in Python

Syntax for using the Python len() office

This is how yous may utilise the len() method of Python:

len(object)

For case, we take a list:

A_list = [5, 10, 15, 20, 'a', 'b', 'c']

This is how you may use the len() method for getting the length of that list:

List_length = len(A_list)

In the length function, you may provide an object that can exist a sequence (listing, tuple, string, bytes or range) or collections such as a dictionary.

See the following examples of using this len() method for getting the length of lists, array, lexicon, and tuple.

An example of getting list length

In this example, a listing is created with a few items. The items are the combination of numbers and string characters. This is followed by using the len() method. The returned value is assigned to a variable and its value is displayed by using the print function. Run across the code and output of this instance:

See online demo and code

Python list length

You can see, the length of the list is returned as 8.

An example of using len method in for loop

In this example, a list of strings is created. After that, a for loop is used to become the length of each string item in the list. Basically, it will not only return the total length of the listing only the length of each string item in the list by using the len() method. Have a look:

Run across online demo and lawmaking

len

Is len() method specific to the list?

No, by definition, the argument of the len() method may be an object e.g. a string, listing, byte, tuple, range etc. Y'all may even provide a collection as a parameter.

So, past using the len() method you may also go the length or the full number of items in a set, dictionary or frozen set etc.

The following section shows examples of using len() method in different sequences and collections.

Getting the length of an array example

The following instance shows how to become the length of an array. First, an array is created afterwards importing the array module. After that, the len() method is used to get the array length which is displayed by using the impress office:

Encounter online demo and code

array length

You can run into, the array which blazon is integer contains five elements.

A len method example with a tuple

Encounter this instance where a tuple is created with string elements. The len() method is used to go the number of items in the tuple:

Meet online demo and code

tuple length

Using len() method with a dictionary

Similarly, y'all may apply the len() method with dictionary drove for getting the total number of items in the lexicon. See the sit-in below:

See online demo and code

dictionary length

Source: https://www.jquery-az.com/5-examples-get-python-list-length-array-tuple-dictionary-also/

Posted by: franksconot1980.blogspot.com

0 Response to "how to find length of list in python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel