On 2018-08-14, Alan Gauld via Tutor <tutor@python.org> wrote: > - Use a list for objects(*) where you might need to > change the value of one of the objects
A list is standard practice when you need an ordered collection of objects of the same type, e.g., a bunch of numbers, a bunch of chickens, etc.. It is most efficient when the list may grow and shrink on the end, but becomes suspicious if it usually grows and shrinks in the middle or at the beginning. Nevertheless, Python lists are very often used even so, and only require moving to a different container type when the builtin list start to bog down. > - Use a tuple for objects where you don't need > to change the values (it remains the same during > the life of the program). Yes, a tuple makes an excellent fixed list. A tuple is typically used to store a fixed-size collection of objects of different types, when those objects have some logical connection to one another. If you had only these two containers to choose from for your original exercise--a collection of colors and word-lengths--it might make sense to store the color and wordlength as a tuple, and to store those tuples in a list, on the assumption that more colors might be added. Aside: In Python there's usually no point in storing the length of a string anywhere, since strings store their own length. > - Use a set where you want to eliminate duplicates Sets are great when the most common operations are adding new items and testing for membership, or when you can take advantage of set operations, like union or symmetric difference. If your list of chickens is unordered and has no duplicates, using a set instead is often a good refinement. -- Neil Cerutti _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor