Skip to content
Advertisement

How do I compare two letters or values in two different lists?

I am making a program that has two lists (in Python), and each list contains 5 different letters. How do I make it so that any index number I choose for both lists gets compared and uppercase a letter if the condition is true? If the first two values in the list are the same (in my case a lowercase letter), then I want the letter in the second list to become uppercase.

example/attempt (I don’t know what I’m doing):

if list1[0] = list2[0]:
   upper(list2[0])

Advertisement

Answer

Without an example of you input and output, it’s difficult to understand what your goal is, but if your goal is to use .upper() on any string in list2 where list1[i] and list2[i] are equal, you can use a combination of zip and enumerate to compare, and then assign the value of list2[i] to the uppercase string like so:

list1 = ['a', 'b', 'c']
list2 = ['a', 'p', 'q']

for i, (x, y) in enumerate(zip(list1, list2)):
    if x == y:
        list2[i] = y.upper()

print(list2)

Output:

['A', 'p', 'q']
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement