Skip to content
Master Python’s Core Data Structures: Lists, Tuples, Dictionaries, and Sets
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

1 List (list)

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

1.1 List creation

>> a = [1, 2.3, True, 'hello']

1.2 Indexing

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

>> a[0]
1
>>> a[3]
'hello'
>>> a[-1]
'hello'

1.3 Modifying elements

Assign a new value to a specific index.

>> a[1] = 'two'
>>> a
[1, 'two', True, 'hello']

1.4 Adding elements

Use append, extend, or insert.

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

1.5 Removing elements

Use pop or remove.

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

2 Tuple (tuple)

Tuples are created with parentheses () and are immutable.

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

3 Dictionary (dict)

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

>> 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'}

4 Set (set)

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

>> 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

5 Common functions

5.1 sum()

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

5.2 len()

>> len(e)
3
>>> len(f)
3
>>> len('hello')
5

5.3 max() and min()

>> max(e)
3
>>> min(f)
5
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

data structuresListfundamentalsdictionarytupleset
Reader feedback

How this landed with the community

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.