课程: Python Practice: Collections
免费学习该课程!
今天就开通帐号,24,700 门业界名师课程任您挑!
Solution: Deduplicate and sort a list
- [Instructor] Here is how I solved this challenge. My solution uses two Python functions to transform the animals list. Working from the inside out here, I'm using the set function to create a new set from the animals list. The set function is a convenient way to make sure we have only unique items in a group. A set cannot contain duplicate items, but a list can. So by converting our list to a set, we get a de-duplicated group of items. Whether we saw one fox or 20 foxes, for example, the set only says fox once. After that, I used the sorted function here to create a new list for my set with its items in alphabetical order. That's important because a set does not have a guaranteed order, but a list does and a requirement of the challenge was to present the items in alphabetical order. This is what I return as the result when my prepare list function is called. Let's run this and see what happens. There's my list.…