How to change n-th element in list of lists with python list comprehensions? -


i have list of sublists that:

posts = [[1, 'text1', 0], [1, 'text2', 0]] 

and function change_text(text)

how can apply function text elements of each sub-list?

i have tried this:

posts = [change_text(post[1]) post in posts] 

but got texts ['changed_text1', 'changed_text2']

you can have list within list comprehension

>>> change_text = lambda x:'changed_'+x >>> posts = [[1, 'text1', 0], [1, 'text2', 0]] >>> [[post[0],change_text(post[1]),post[2]] post in posts] [[1, 'changed_text1', 0], [1, 'changed_text2', 0]] 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -