I need to print out a list differently depending on how many items it has:
For example:
- For no items i.e.
[]should output{} - For 1 item i.e.
["Cat"]should output{Cat} - For 2 items i.e.
["Cat", "Dog"]should output{Cat and Dog} - For 3 or more items i.e.
["Cat", "Dog", "Rabbit", "Lion"]should output{Cat, Dog, Rabbit and Lion}
I currently am doing something like this with a bunch of if statements:
def customRepresentation(arr):
if len(arr) == 0:
return "{}"
elif len(arr) == 1:
return "{" + arr[0] + "}"
elif len(arr) == 2:
return "{" + arr[0] + " and " + arr[0] + "}"
else:
# Not sure how to deal with the case of 3 or more items
Is there a more pythonic way to do this?