How to find index of tuple in list of tuples in Python? -


i have list of tuples in python,

a = [('foo', 3), ('bar', 1)] 

and want modify second element in tuple containing 'foo'. more specifically, increment number. in other terms, want do

>>> increment(a, 'foo') >>> print [('foo', 4), ('bar', 1)] 

you can't directly change value within tuple (tuples immutable). however, can replace element new tuple looks old one:

def increment(a, name):     i, x in enumerate(a):         if x[0] == name:             a[i] = (x[0], x[1] + 1)             break 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -