Stuuuve
Stuuuve

Reputation: 59

Error while concatenating lists in python

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!

Upvotes: 1

Views: 97

Answers (1)

bananafish
bananafish

Reputation: 2917

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.

Upvotes: 7

Related Questions