Understanding Advance Tuples using Functions

A code to add multiple numbers using Python programming.

Welcome to the code! Here’s a slightly advance code to understand and have fun with tuples using functions in python programming.

In this code, we are defining a function onehop(). The function works like this. It takes as input a list of pairs. It the list of pairs contain (i,j) and (j,k), the function must group them as (i,j).

For example,
onehop([(2,3),(1,2)]) is [(1, 3)]. Similarly, onehop([(2,3),(1,2),(3,1),(1,3),(3,2),(2,4),(4,1)]) is
[(1, 2), (1, 3), (1, 4), (2, 1), (3, 2), (3, 4), (4, 2), (4, 3)].

For this code, we will be using the concept of a function for defining the given function. “not in” operator is used to check if an element is present or not present in the list.

Note: Loved the post? You too can publish your article on “Python for fun” which will be loved by millions. Contribute an article!

					def onehop(l):
    nw = []

    for i in range(len(l)):
        for j in range(len(l)):
            if l[i] != l[j]:
                if l[i][1] == l[j][0]:
                    a = l[i][0]
                    b = l[j][1]
                    if a != b:
                        t = [a, b]
                        t = tuple(t)
                        if t not in nw:
                            nw = nw + [tuple(t)]

    return (nw)

l = []
li = input("Enter the group of two tuples:\n")  # for taking an input of tuples from the user

while (li != ""):

    li = input()



Share on facebook
Share on whatsapp
Share on pinterest
0 0 votes
Notify of
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
5 months ago

thank u brother