Skip to content

Python Tuple Length with Example Malli Spark By {Examples}

  • by

How to find or get the length of a tuple in Python? To get the length of a tuple, you can use the built-in len() function. The len() function is used to find the length of a tuple, which is equivalent to the size of the tuple in terms of the number of elements it contains. In this article, I will explain the syntax of the tuple len() method, and its parameters and explain how to use it.

1. Quick Examples of Length of Tuple

If you are in a hurry, below are some quick examples of how to get the length of a tuple.

# Quick examples of length of tuple

# Example 1: Get length of tuples
# Using len() function
technology= (‘Spark’,’Pandas’,’Pyspark’,’Java’)
result = len(technology)

# Example 2: Get length of tuples
numbers = (10, 6, 13, 19, 25, 12)
result = len(numbers)

# Example 3: Check if a tuple is empty in python
tuples = ()
if len(tuples) == 0:
print(“The tuple is empty”)
else:
print(“The tuple is not empty”)

# Example 4: Using not operator
tuples = ()
if not tuples:
print(“The tuple is empty”)
else:
print(“The tuple is not empty”)

2. Syntax of tuple len() Method

Following is a syntax of the len() method.

# Syntax of len() method
len(tuple)

2.1 Parameters of Length

tuple – the tuple can be a sequence (such as a string, tuple, or list) or a collection (such as a dictionary or set) whose length you want to find.

2.2 Return Value

The len() function is used to return the number of elements in the tuple.

3. Get Length of Tuples Using len() Function

You can get the length (i.e., the number of elements) of a tuple using the built-in len() function. For example, you can create a tuple with 4 elements, and then you use the len() function to get the length of the tuple.

# Using len() function
technology= (‘Spark’,’Pandas’,’Pyspark’,’Java’)
result = len(technology)
print(“Get length of tuples:”, result)

# Output:
# Get length of tuples: 4

Let’s look at another example of tuple that takes numbers and use the len() function to get the size (or length) of the tuple numbers, which contains six elements.

# Get length of tuples
numbers = (10, 6, 13, 19, 25, 12)
result = len(numbers)
print(“Get length of tuples:”, result)

# Output
# Get length of tuples: 6

4. Check if a Tuple is Empty in Python

You can check if a tuple is empty in Python by using the built-in len() function, which returns the length of a tuple. For example, you first define an empty tuple tuples using empty parentheses (). Then you can use the len() function to check the length of the tuple.

# Check if a tuple is empty in python
tuples = ()
if len(tuples) == 0:
print(“The tuple is empty”)
else:
print(“The tuple is not empty”)

# Output
# The tuple is empty

If the length of the tuple is 0, then the tuple is empty and you print a message indicating that. Otherwise, you print a message indicating that the tuple is not empty.

You can also use a boolean evaluation of the tuple directly to check if it’s empty. For example, not tuples statement will evaluate to True if the tuple is empty, and False otherwise.

This is because an empty tuple evaluates to False in a boolean context.

# Using not operator
tuples = ()
if not tuples:
print(“The tuple is empty”)
else:
print(“The tuple is not empty”)

# Output
# The tuple is empty

Conclusion

In this article, I have explained the python tuple len() method syntax, parameters, and how to use it to find the length of a tuple. Also, learned other ways to find the tuple is length.

Happy Learning !!

 How to find or get the length of a tuple in Python? To get the length of a tuple, you can use the built-in len() function. The len() function is used to find the length of a tuple, which is equivalent to the size of the tuple in terms of the number of elements it  Read More Python, Python Tutorial, python tuple examples 

Leave a Reply

Your email address will not be published. Required fields are marked *