Thursday, May 9, 2024
HomePythonMethods to Change Tuple Values in Python? – Finxter

Methods to Change Tuple Values in Python? – Finxter


Drawback Formulation and Resolution Overview

This text will present you tips on how to change tuple values in Python.

To make it extra fascinating, we’ve the next working state of affairs:

Eddie, the Meteorologist at NTCV, has calculated an incorrect 5 (5) Day climate forecast. These values are saved as a Tuple and require updating.


💬 Query: How would we write code to alter these Tuple values?

We are able to accomplish this activity by one of many following choices:


Technique 1: Use Slicing

This instance makes use of slicing together with the addition operator + to replace an present tuple aspect and insert a further aspect.

nums_5day = (11, 12, 13, 14, 15)
nums_6day = nums_5day[:2] + (18, 21) + nums_5day[3:]
print(nums_6day)

The above code declares a Tuple containing projected temperatures for the subsequent 5 (5) days. The outcomes save to nums_5day.

The next line updates this Tuple by changing a component and inserting a further aspect utilizing slicing. Thus making it projected temperatures for the subsequent six (6) days. The outcomes save to nums_6day and are output to the terminal.


Technique 2: Use Unpack and Slicing

This instance makes use of the unpack operator (*) and slicing to switch one (1) worth from an present tuple.

nums_5day = (11, 12, 13, 14, 15)
new_5day = (30, *nums_5day[1:])
print(new_5day)

The above code declares a Tuple containing projected temperatures for the subsequent 5 (5) days. The outcomes save to nums_5day.

The next line replaces the present temperature of 11 to 30. Then, the unpack (*) operator and slicing are used to maintain the remaining parts. The outcomes save to new_5day and are output to the terminal.


Technique 3: Use Record and Record Comprehension

This instance makes use of listing and listing comprehension to switch one (1) tuple worth.

nums_5day = listing((11, 12, 13, 14, 15))
new_5day = tuple([x + 5 if x == 13 else x for x in nums_5day])
print(new_5day)

The above code declares a Tuple containing projected temperatures for the subsequent 5 (5) days. That is transformed to a listing and saved to nums_5day.

The next line makes use of listing comprehension to switch the listing aspect with the worth of 13 to 18 (x+5). The outcomes are transformed to a Tuple, saved to new_5day and output to the terminal.


Technique 4: Use map() and a lambda

This instance makes use of the map() perform and a lambda to switch three (3) tuple values.

nums_5day = listing((11, 12, 13, 14, 15))
new_5day = tuple(map(lambda x: x+3 if x >= 12 and x <= 14 else x, nums_5day))
print(new_5day)

The above code declares a Tuple containing projected temperatures for the subsequent 5 (5) days. That is transformed to a listing and saved to nums_5day.

The next line calls the map() perform and passes it one (1) argument, a lambda. This consequence returns a map object. If this object was output to the terminal, an object reference much like beneath would show.

<map object at 0x0000021A7F45B190>

This object is then transformed from the map object to a Tuple and output to the terminal.


Abstract

This text has supplied 4 (4) methods to alter Tuple values to pick the most effective match to your coding necessities.

Good Luck & Comfortable Coding!


Programming Humor – Python

“I wrote 20 brief packages in Python yesterday. It was fantastic. Perl, I’m leaving you.”xkcd

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments