In python we have get() method in dictionary to get values safely. Any way to achieve this in list?
my_dict = {'fruit':'apple','colour':'black'}
my_dict.get('colour','None') #returns black
my_dict.get('veg','None') #returns None
my_list = ['apple','fish','blue','bottle','gold']
colour = my_list[2]
print(colour) #returns blue
value = my_list[7] #gives error. Expected: need to return default value as None.
In my case length of my_list is not constant.Values are getting populated in runtime.If i try to get the index which is not present in the my_list it should return default value as None. How to get the default values when list is not having requested index.?