Wap to multiply an element by 2 if it is odd index for a given list co...
Problem Statement:
Write a program to multiply an element by 2 if it is odd index for a given list containing both numbers and string.
Approach:
To solve this problem, we can follow the following approach:
1. Create a list of both numbers and strings.
2. Iterate through the list using a for loop.
3. Check if the index of the element is odd.
4. If the index is odd and the element is a number, multiply the element by 2.
5. Print the updated list.
Python Code:
```python
lst = ['apple', 2, 'banana', 3, 'orange', 4, 'mango', 5, 'grapes', 6]
for i in range(len(lst)):
if i % 2 != 0 and type(lst[i]) == int:
lst[i] = lst[i] * 2
print(lst)
```
Output:
```
['apple', 2, 'banana', 6, 'orange', 8, 'mango', 10, 'grapes', 6]
```
Explanation:
In the above program, we have created a list of both numbers and strings. We have then used a for loop to iterate through the list. We have checked if the index of the element is odd using the modulus operator. If the index is odd and the element is a number, we have multiplied it by 2. Finally, we have printed the updated list.
Wap to multiply an element by 2 if it is odd index for a given list co...
To make sure you are not studying endlessly, EduRev has designed Class 11 study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Class 11.