1

I am working on an assignment for one of my classes at school where I need to concatenate two lists together. I am using the code:

flowers = ["rose", "bougainvillea", "yukka", "marigold", "daylily", "lily of the valley"]

thorny = flowers[0:3]
poisonous = flowers[-1]
dangerous = flowers[0:3] + flowers[-1]

I keep getting the error message:

dangerous = list(set(flowers[0:3] + flowers[-1]))   
TypeError: can only concatenate list (not "str") to list

I was wondering why this doesn't work. Thanks!

1
  • dangerous = flowers[0:3] + flowers[-1:] Commented Sep 6, 2016 at 4:41

1 Answer 1

7

flowers[0:3] returns a list while flowers[-1] returns a string, so you are adding a string to a list. You can use flowers[-1:] to return a list instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.