KnowledgeBoat Logo

Computer Science

Write a program to multiply an element by 2 if it is an odd index for a given list containing both numbers and strings.

Python

Python List Manipulation

5 Likes

Answer

mixed = eval(input("Enter the list: "))
for index in range(1, len(mixed), 2):
        mixed[index] *= 2  
print("Modified List:", mixed)

Output

Enter the list: [1, 'apple','banana', 5, 'cherry', 7, 'date', 'cherry'] 
Modified List: [1, 'appleapple', 'banana', 10, 'cherry', 14, 'date', 'cherrycherry']

Answered By

1 Like


Related Questions