Computer Science, asked by ashahyndavi25, 11 days ago

b. Write a Python program to replace last value of tuples in a list. Sample list: [(10, 20, 40), (40, 50, 60), (70, 80, 90)] Expected Output: [(10, 20, 100), (40, 50, 100), (70, 80, 100)]​

Answers

Answered by poojan
2

Replacing the last values of tuples in a list.

Language used: Python Programming

Program:

#The given list is:

sample_list = [(10, 20, 40), (40, 50, 60), (70, 80, 90)]

#Iterating over each tuple in a list

for i in range(len(sample_list)):

   #tuple is immutable. So, to change the values, let's convert the tuple into list for time being

   upd_list=list(sample_list[i])

   #changing the last value in each tuple turned list to 100 on each iteration

   upd_list[-1] = 100

   #After the updation, turn the updated list to tuple and assign it back to the sample list

   sample_list[i]=tuple(upd_list)

#printing the updated list

print(sample_list)

Output:

[(10, 20, 100), (40, 50, 100), (70, 80, 100)]

Learn more:

Write a statement to add a Key: Value Pair of 7:49 to dictionary  D={1:1,3:9,5:25}

brainly.in/question/43499112

Your friend and you made a list of topics and both of them voted if they like or dislike the topic. They wrote 0 to denote dislike and 1 to denote like..

brainly.in/question/44681897

Attachments:
Similar questions