Data structure converter function
Now you've learned about the types of arguments in functions, you'll put this into practice by building a custom function that converts data into different structures.
This exercise is part of the course
Intermediate Python for Developers
Exercise instructions
- Define
convert_data_structure
with two arguments:data
anddata_type
, where the latter has a default value of"list"
. - Add a condition to check if
data_type
is"tuple"
. - Else if
data_type
is"set"
, convertdata
into a set, saving it as a variable of the same name. - Call the function on the data structure provided, using an appropriate keyword argument value-pair to convert it to a set.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create the convert_data_structure function
def ____(____, ____):
# If data_type is "tuple"
____ ____ ____ "____":
data = tuple(data)
# Else if data_type is set, convert to a set
____ ____ ____ "____":
____ = ____(____)
else:
data = list(data)
return data
# Call the function to convert to a set
____({"a", 1, "b", 2, "c", 3}, ____="____")