Python program to merge two primitives

Python Program to Merge Two Graphs

In Python, a tuple is an immutable sequence type, commonly used to store collections of items.

In Python, we use parentheses to define a tuple, which includes the data we want to store.

Var = (1, 'a', 3.7)

Sometimes, we must use a single variable that holds two or more elements from different graphs. In these cases, we must know how to merge them to create a single variable.

In this article, we’ll discuss how to combine two elements in Python

Using the + Operator

As we know, the Python programming language provides us with a variety of operators and functions that allow us to easily perform complex tasks without requiring much technical knowledge.

Using the + operator is a great example, as it can be used not only to concatenate elements but also to concatenate strings and lists. This is the simplest way to combine two elements. The steps in this process are very simple.

Step 1 – Create two primitives

Step 2 – Create a new tuple and assign the sum of the two tuples as its value.

Example

A = (1, 2, 3)
B = ('a', 'b', 'c')
print("The first tuple, A : ", A)
print("The second tuple, B : ", B)
C = A + B
print("The resulting tuple is, C : ", C)

Output

The first tuple, A : (1, 2, 3)
The second tuple, B : ('a', 'b', 'c')
The resulting tuple is, C : (1, 2, 3, 'a', 'b', 'c')

Using sum()

Another way to generate concatenated tuples is to use the default sum() method in Python. Let’s first look at what the sum() function is and how it’s used.

The sum function in Python takes an iterable, such as a list or tuple, and returns the sum of its elements. In this example, we’ll provide it with a tuple of tuples—a nested tuple whose elements will be the tuples we want to concatenate. The sum function will return a single tuple containing all the concatenated elements.

Example

A = (1, 2, 3)
B = ('a', 'b', 'c')
print("The first tuple, A : ", A)
print("The second tuple, B : ", B)
C = sum((A, B), ())
print("The resulting tuple is, C : ", C)

Output

The first tuple, A : (1, 2, 3)
The second tuple, B : ('a', 'b', 'c')
The resulting tuple is, C : (1, 2, 3, 'a', 'b', 'c')

Concatenation Using the List Extend Method

We’ll use type conversion to convert the element into a list using the list method. Then, we’ll use the list object’s extend method to concatenate the two lists we created.

The extend method iterates over an iterator and appends items to the end of the first iterator. After converting the resulting list into a tuple, we’ll store the result in a variable.

Algorithm

Step 1 – Create two tuples with some elements

Step 2 – Print the elements of two different primitives

Step 3 – Convert the primitive into a list using the list method

Step 4 – Create a concatenated list using the extend method

Step 5 – Convert the resulting list into a tuple and then print it

Example

A = (1, 2, 3)
B = ('a', 'b', 'c')
print("The first tuple, A : ", A)
print("The second tuple, B : ", B)
listA = list(A)
listB = list(B)
listA.extend(listB)
C = tuple(listA)
print("The resulting tuple is, C : ", C)

Output

The first tuple, A : (1, 2, 3)
The second tuple, B : ('a', 'b', 'c')
The resulting tuple is, C : (1, 2, 3, 'a', 'b', 'c')

Summary

In this article, we focused on three different methods for merging two elements. We learned about the Pythonic method, which is simple and easy to use. We also saw how to use the default Python method sum() to achieve similar results. Finally, we saw how to use type conversion and the list method extend() to perform concatenation.

Leave a Reply

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