Fundamentals 5 min read

Master Python’s Core Data Structures: Lists, Tuples, Dictionaries, and Sets

Explore Python’s fundamental composite data structures—lists, tuples, dictionaries, and sets—covering creation, indexing, modification, common methods, and built‑in functions like sum, len, max, and min, with clear code examples illustrating each operation in practice.

Model Perspective
Model Perspective
Model Perspective
Master Python’s Core Data Structures: Lists, Tuples, Dictionaries, and Sets

1 List (list)

Lists are created with square brackets [] and are ordered, mutable sequences.

1.1 List creation

<code>>> a = [1, 2.3, True, 'hello']</code>

1.2 Indexing

Python uses zero‑based indexing; the first element is 0 , the last can be accessed with -1 .

<code>>> a[0]
1
>>> a[3]
'hello'
>>> a[-1]
'hello'</code>

1.3 Modifying elements

Assign a new value to a specific index.

<code>>> a[1] = 'two'
>>> a
[1, 'two', True, 'hello']</code>

1.4 Adding elements

Use append , extend , or insert .

<code>>> a.append(10)
>>> a.extend([30, 40, 50])
>>> a.insert(2, False)
>>> a
[1, 'two', False, True, 'hello', 10, 30, 40, 50]</code>

1.5 Removing elements

Use pop or remove .

<code>>> a.pop(3)   # removes element at index 3
>>> a.remove(30)
>>> a
[1, 'two', False, True, 'hello', 10, 40, 50]</code>

2 Tuple (tuple)

Tuples are created with parentheses () and are immutable.

<code>>> b = (1, [3, 4], 'five')
>>> b[1][1]
4
>>> b[2] = 5   # raises TypeError because tuples cannot be modified</code>

3 Dictionary (dict)

Dictionaries are defined with curly braces {} using key:value pairs.

<code>>> c = {1:'Mon', 2:'Tues', 3:'Wed', 4:'Thu', 5:'Fri'}
>>> c[2]
'Tues'
>>> c[3] = 'W'
>>> c[6] = 'Sun'   # add new key
>>> c.pop(1)       # remove key 1
>>> c
{2:'Tues', 3:'W', 4:'Thu', 5:'Fri', 6:'Sun'}</code>

4 Set (set)

Sets are unordered collections of unique elements, created with curly braces {}.

<code>>> d = {1, 2, 3, 4}
>>> 1 in d
True
>>> d.pop()        # removes an arbitrary element
>>> d.remove(2)
>>> d1 = d | {7, 8} # union
>>> d & {1, 4}      # intersection</code>

5 Common functions

5.1 sum()

<code>>> e = [1, 2, 3]
>>> sum(e)
6
>>> f = (5, 6, 7)
>>> sum(f)
18
>>> j = {9, 10, 11}
>>> sum(j)
30</code>

5.2 len()

<code>>> len(e)
3
>>> len(f)
3
>>> len('hello')
5</code>

5.3 max() and min()

<code>>> max(e)
3
>>> min(f)
5</code>
pythonData Structureslistfundamentalsdictionarytupleset
Model Perspective
Written by

Model Perspective

Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.