[Tutor] Replace nth item with single value in each nested list
Mark Lawrence
breamoreboy at gmail.com
Thu May 14 16:50:28 EDT 2020
On 14/05/2020 15:28, EK Esawi via Tutor wrote:
> Hi all--
>
>
> Thank you Mats for the help.
>
> On my previous post, I did not state my question accurately. The question is how do I replace the 2nd element in each sublist based on a condition? For example, replace the 2nd element with 11 in each sublist if it’s greater than 5. If a=[[1,2],[4,5],[7,8],[3,6]] then the desired results would be a=[[1,2],[4,5],[7,11],[3,11]]. if possible via list comprehension
>
> Thanks again--EK.
>
Why not extend Mats' example?
for sub in a:
if sub[1] > 5:
sub[1] = 11
Why the list comprehension, what do you gain, if anything, from using one?
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Tutor
mailing list