0% found this document useful (0 votes)
2 views2 pages

Week 2 Notes

The document compares five data structures in Python: Array, Set, Tuple, List, and Dictionary, highlighting their definitions, mutability, order, duplicates, indexing, slicing, type constraints, syntax, access methods, use cases, performance, memory efficiency, and examples. Key differences include the Array being for homogeneously typed data, Set for unordered unique collections, Tuple for immutable ordered collections, List for mutable ordered collections, and Dictionary for mapping unique keys to values. This comparison serves to clarify the appropriate use cases for each data structure.

Uploaded by

pajeg11558
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Week 2 Notes

The document compares five data structures in Python: Array, Set, Tuple, List, and Dictionary, highlighting their definitions, mutability, order, duplicates, indexing, slicing, type constraints, syntax, access methods, use cases, performance, memory efficiency, and examples. Key differences include the Array being for homogeneously typed data, Set for unordered unique collections, Tuple for immutable ordered collections, List for mutable ordered collections, and Dictionary for mapping unique keys to values. This comparison serves to clarify the appropriate use cases for each data structure.

Uploaded by

pajeg11558
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Notes

Feature Array Set Tuple List Dictionary

Definition Fixed-type Unordered Ordered, Ordered, Unordered


sequence of collection of immutable mutable collection of
elements. unique sequence of sequence of key-value
elements. elements. elements. pairs.

Mutable Yes Yes No Yes Keys: No,


Values: Yes

Order Yes No Yes Yes No (insertion


order preserved
from Python
3.7+)

Duplicates Yes No Yes Yes Keys: No,


Values: Yes
Indexing Supported Not supported Supported Supported Not applicable

Slicing Supported Not supported Supported Supported Not applicable

Heterogeneous No Yes Yes Yes Yes (keys and


Elements values)

Type Must be of No type No type No type Keys must be


Constraints same type constraint constraint constraint immutable
(e.g., strings,
tuples)

Syntax array('i', [1, {1, 2, 3} (1, 2, 3) [1, 2, 3] {1: 'a', 2: 'b'}


2, 3])

Access Method Via index Via iteration Via index Via index Via key

Use Cases For fixed- Membership Immutable General- Lookups by


type tests, removing ordered purpose key, storing
numerical duplicates collections mutable related data
data sequence

Performance Efficient for Fast Fast read-only Slower than Fast lookups
numerical membership access tuples for for keys
operations tests read-only

Memory More Depends on size More Consumes Memory use


Efficiency efficient for and number of memory- more memory depends on
elements than tuples
numeric efficient than key-value pair
types lists size

Example arr = s = {1, 2, 3} t = (1, 2, 3) l = [1, 2, 3] d = {'a': 1, 'b':


array('i', [1, 2}
2, 3])

Key Differences:
• Array is used for homogeneously typed data (e.g., all integers or all floats).
• Set is used for unordered collections where uniqueness is important.
• Tuple is immutable and ordered, used for fixed collections of elements.
• List is mutable and ordered, suitable for general-purpose collections.
• Dictionary is used for mapping unique keys to values, ideal for lookups.

You might also like