[Tutor] Replace nth item with single value in each nested list
Alan Gauld
alan.gauld at yahoo.co.uk
Thu May 14 17:32:18 EDT 2020
On 14/05/2020 15:28, EK Esawi via Tutor wrote:
> 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
Why do you want to use a list comprehension? They are used for creating
new lists not modifying existing ones. It rather sounds like a homework
exercise - a bad homework exercise at that. We don't do homework for
you, although we can offer suggestions.
But trying to force this into a list comprehension when it doesn't
naturally fit is a strange request. It can be done but it wouldn't
be very idiomatic Python.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list