lists-tuples-sets-dictionaries

Contents

Roadmap info from roadmap website

Lists, Tuples, Sets, and Dictionaries

Lists: are just like dynamic sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists need not be homogeneous always which makes it the most powerful tool in Python.

Tuple: A Tuple is a collection of Python objects separated by commas. In some ways, a tuple is similar to a list in terms of indexing, nested objects, and repetition but a tuple is immutable, unlike lists that are mutable.

Set: A Set is an unordered collection data type that is iterable, mutable, and has no duplicate elements. Python’s set class represents the mathematical notion of a set. s Dictionary: In python, Dictionary is an ordered (since Py 3.7) [unordered (Py 3.6 & prior)] collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more optimized.

Visit the following resources to learn more:

FeatureListsTuplesSetsDictionaries
Syntax[1, 2, 3](1, 2, 3){1, 2, 3}{'key1': 'value1', 'key2': 'value2'}
OrderedYesYesNoYes (insertion order since Python 3.7)
MutableYesNoYesYes
Allows DuplicatesYesYesNoKeys: No, Values: Yes
IndexingYesYesNoKeys used for accessing values
HeterogeneousYesYesYesYes
Use CaseOrdered collection of itemsImmutable ordered collectionUnordered collection of unique itemsCollection of key-value pairs
Methods AvailableMany (e.g., append, pop, sort)Few (e.g., count, index)Few (e.g., add, remove, union)Many (e.g., keys, values, items, get)
Memory EfficiencyLess efficient than tuplesMore efficient than listsVaries (no duplicates, so potentially more efficient for unique items)Depends on the size and hash function of keys
IterationYesYesYesYes
Creation[1, 2, 3](1, 2, 3){1, 2, 3}{'key': 'value'}
ComprehensionsList comprehensions ([x for x in iterable])Not directly, but can cast to list comprehensionSet comprehensions ({x for x in iterable})Dictionary comprehensions ({k: v for k, v in iterable})
Key CharacteristicsFlexible, mutable, and allows duplicatesImmutable, can be used as keys in dictionariesUnordered, no duplicates allowedMaps unique keys to values
#reviewed #python #online #programming-language #loops #data-structures #ready