Computer Science, asked by deepaksai4819, 1 year ago

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 ridhimakh1219
6

Write a python program to replace last value of tuples in a list

Explanation:

Python program to replace last value of tuples in a list

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

print([t[:-1] + (100,) for t in lst])

Output

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

Example to create List

lis = [1,2,3,4]

print(lis)

Output

[1,2,3,4]

Example to create Tuple

tup = (1,2,3,4)

print(tup)

Output

(1,2,3,4)

Answered by poojan
14

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