Bag as A Data Structure

Bag is one of the fundamental data structures worthy of note, albeit less often mentioned like set, sequence, queue, stack, linked list, etc.

Image from Algorithms 4th edition by Robert Sedgewick, Kevin Wayne

A bag is an unordered collection of items, which may contain duplicate items.

As its name nicely suggests, one can add items to it, and iterate over all items (hence an iterable). But it does not support remove API, like, a piggy bank.

From Unsplash

A bag also allows duplicates, unlike its cousin set. Because of this, it is sometimes also called multiset.

Comparison of set, sequence and bag from this

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store