x = ["Artificial Intelligence","Digital Graphic Design","PE","A"]
def select_second_element(x):
if len(x)<2:
return x[1]
else:
return None
CodePudding user response:
Seeing your comments you probably mean "How do I get the second character from each string?" (a string is just a list of character elements).
Changing to >=
as j1-lee suggested calling that method in a loop for each element will give you that result.
def select_second_element(lst):
return lst[1] if len(lst) >= 2 else None
for element in x:
print(select_second_element(element))
Returns:
r
i
E
None
CodePudding user response:
Just try that for any function, note that you should consider indexing
def select_second_element(x):
try:
return x[1]
except IndexError:
print('There is no second element')