Dear Python coders, I am new to python programming and I am trying to solve an assignment.
The question is stated below;

Create a function that does the following
1. Accepts as the first parameter a string specifying the data structure to be used "list", "set" or "dictionary"
2. Accepts as the second parameter the data to be manipulated based on the data structure specified e.g [1, 4, 9, 16, 25] for a list data structure
3. Based off the first parameter
return the reverse of a list or
add items "ANDELA", "TIA" and "AFRICA" to the set and return the resulting set
return the keys of a dictionary.

def manipulate_data(argument1, argument2):
  if argument1 == "list":
  return argument2

  if argument1 == "set":
  return argument2

  if argumet1 == "dictionary":
  return argument2

My code above has not worked. It generates error.
Please someone help me check this code to know if I am taking the right step. Correct my code with example please.
Quick response will be highly appreciated.

It seems implicit to me that the code should keep argument2 unchanged and return a new value (new list or set). I suggest

def manipulate_data(typename, instance):
    if typename == 'list':
        result = instance[::-1]
    elif typename == 'set':
        result = instance | set(['ANDELA','TIA','AFRICA'])
    elif typename == 'dictionary':
        result = list(instance.keys())
    return result

Edited 8 Months Ago by Gribouillis

This article has been dead for over six months. Start a new discussion instead.