list of the lists - append after search

gvmcmt at gmail.com gvmcmt at gmail.com
Sun Mar 5 11:09:01 EST 2017


On Thursday, March 2, 2017 at 9:33:14 PM UTC+5:30, Andrew Zyman wrote:
> Hello,
>  please advise.
> 
>  I'd like search and append the internal list in the list-of-the-lists.
> 
> Example:
>  ll =[ [a,1], [b,2], [c,3], [blah, 1000] ]
> 
>  i want to search for the internal [] based on the string field and, if matches, append that list with a value.
> 
>   if internal_list[0] == 'blah':
>       ll[ internal_list].append = [ 'new value']
> 
> End result:
>  ll =[ [a,1], [b,2], [c,3], [blah, 1000, 'new value'] ]
> 
> 
> I came up with the following, but the second stmnt is not correct:
> 
> print [x for x in ll if x[0]== 'blah']
> print ll.index([x for x in ll if x[0]=='blah'])
> 
> output:
> ValueError: [['blah', 1]] is not in list
> 
> 
> 
> 
> thank you
> AZ


list_of_lists = [['a', 1], ['b', 2], ['c', 3], ['blah', 1000]]

for sublist in list_of_lists:
    if sublist[0] == 'blah':
        sublist.append('new value')


Hope that helps.



More information about the Python-list mailing list