Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I want to compare the first character of two different strings (and so on) to form a new string based on those results. This is what I've tried using, however, it's comparing every element of each list to each other.

def compare(a,b):

    s = ""

    for x in a:

        for y in b:

            if x == y:

                s+=str(x)

            else:

                s+=str(y)

It seems like such a simple question but I'm stuck.

1 Answer

0 votes
by (107k points)

Kindly use the zip() to achieve your result:

def compare(a, b):

    for x, y in zip(a, b):

        if x == y:

            ...

For more information, kindly refer to the Python certification

Related questions

0 votes
1 answer
asked Jul 13, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
0 votes
2 answers
asked May 6, 2020 in SQL by Sudhir_1997 (55.6k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...