How can I access the strings of 'X'
list = [['X','Y','Z'], ['X','Z','Y']]
For example I want to define a function to return True if list[1] of both list is equal to 'X'
You can use all to see if all ith elements in each sublist are the same:
def is_equal(l, i):
first = l[0][i]
return all(sub[i] == first for sub in l)
You might want to catch an IndexError in case i is outside the bounds of and sublist:
def is_equal(l, i):
try:
first = l[0][i]
return all(sub[i] == first for sub in l)
except IndexError:
return False
If you want to explicitly pass a value to check:
def is_equal(l, i, x):
try:
return all(sub[i] == x for sub in l)
except IndexError:
return False
i was outside the bounds of any sublist
all('X' in x[0] for x in your_list)all('X' in x[0] for x in your_list)is wrong,"X" in "FOO X"is True