Searching for elements in a python list and then accessing particular values
Mylist = [('A',3,['x','y','z']),('B',2,['m','n'])]
Variable = 'A'
i = Mylist.index(Variable)
print i
For example I would like to check if is Variable is present and access its elements like is present then access each of sublist elements one by one
For instance in the above example I would like to check if 'A' is present and if yes then get access to its individual sublist elements like 'x' , 'y' and 'z'
It looks like you haven't found the Python dict yet. Perhaps you mean:
mydict = {}
mydict['A'] = {'count': 3, 'letters': ['x','y','z']}
mydict['B'] = {'count': 2, 'letters': ['m','n']}
mydict is a dictionary that itself contains dictionaries.
You can then look them up with:
val = mydict['A']
print val['letters']
Of course, there's little point in storing 'count' since you can just use len() if you need to. You could do it like so:
mydict = {'A': ['x','y','z'], 'B': ['m','n']}
print mydict['A']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With