I do not think you understand what you are trying to do.
What you have listed.
- Take a parameter specifying what data type is being passed
- Takes a second parameter the data to be manipulated
- Return the reverse of the list
- Add “ANDELA”, “TIA” and “AFRICA” to a set
- Return the keys of a dict
Ok so let’s do that then,
def manipulate_data(data_type=None, data=None):
if data_type is 'list':
return data[-1:: -1]
if data_type == 'set':
return set.union(data, ["ANDELA", "TIA", "AFRICA"])
if data_type == 'dict':
return [key for key, item in data.items()]
The above function does what you pointed out that you wanted to accomplish.
If you want to add error checking to this it would be simple,
Code: with error checking
def manipulate_data(data_type=None, data=None):
try:
if data_type is 'list':
return data[-1:: -1]
if data_type == 'set':
return set.union(data, ["ANDELA", "TIA", "AFRICA"])
if data_type == 'dict':
return [key for key, item in data.items()]
else:
raise TypeError
except TypeError as e:
print("\nYou failed to pass the correct data type.\n%s\n" % e)
Or we can do some fancy stuff like this, so we can expand out accepted data types easy as pie.
Code: with ability to easily add data types
def manipulate_data(data_type=None, data=None):
accepted_data_types = {
'list': 'data[-1:: -1]',
'set': 'set.union(data, ["ANDELA", "TIA", "AFRICA"])',
'dict': '[key for key, item in data.items()]'
}
try:
return eval(accepted_data_types['%s' % data_type])
except KeyError as e:
print("\nYou failed to pass the correct data type.\n%s\n" % e)
Now we have a function that we can build on easily.
If this is not what you need feel free to add you what you have so that we can provide some assistance.