Fundamentals 11 min read

Python Data Structures: Lists, Tuples, Dictionaries, and Sets with Practical Code Examples

This article introduces Python's core built‑in sequence types—lists, tuples, dictionaries, and sets—explaining their characteristics, common operations, and performance considerations, and provides numerous interactive code snippets that demonstrate creation, indexing, slicing, modification, searching, sorting, and set algebra.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Data Structures: Lists, Tuples, Dictionaries, and Sets with Practical Code Examples

Data structures are the ways computers store and organize data; in Python, sequences are the most basic structures, where each element is assigned an index.

Python provides several built‑in sequence types: list , tuple , the range function, str (text), binary types ( bytes , bytearray , memoryview ), set / frozenset , and dict . The article walks through each type with examples.

1. List

Lists store mutable elements. Common operations demonstrated include appending, extending, inserting, indexing, slicing, modifying, deleting, and sorting.

In [1]: friends = []
In [2]: friends.append('MissSun')
In [3]: friends
Out[3]: ['MissSun']
In [4]: friends.extend(['Escape', 'Tom'])
In [5]: friends
Out[5]: ['MissSun', 'Escape', 'Tom']
In [20]: friends.insert(1, 'Wang')
In [21]: friends
Out[21]: ['MissSun', 'Wang', 'Escape', 'Tom']
In [22]: len(friends)
Out[22]: 3
In [23]: friends[2]
Out[23]: 'Escape'
In [24]: friends[1:3]
Out[24]: ['Wang', 'Escape']
In [25]: friends[0] = 'MrSun'
In [26]: friends
Out[26]: ['MrSun', 'Wang', 'Escape', 'Tom']
In [27]: friends.remove('Wang')
In [28]: friends
Out[28]: ['MrSun', 'Escape', 'Tom']
In [29]: friends.sort()
In [30]: friends
Out[30]: ['Escape', 'MrSun', 'Tom']

2. Tuple

Tuples store immutable elements and are often used for fixed collections or as dictionary keys. The article shows conversion between lists and tuples, performance comparison, and tuple methods.

In [1]: tup = (1, 2)
In [2]: list(tup)
Out[2]: [1, 2]
In [3]: tuple([1, 2])
Out[3]: (1, 2)
In [4]: import timeit
In [5]: timeit.timeit('["Escape", "MissSun", "Bob", "Lucy"]')
Out[5]: 0.064e-6  # list creation
In [6]: timeit.timeit('("Escape", "MissSun", "Bob", "Lucy")')
Out[6]: 0.0147e-6  # tuple creation (faster)
In [7]: tup = (1, 2, [3, 4])
In [8]: tup[2] += [5, 6]  # raises TypeError but list inside tuple is mutated
Out[8]: (1, 2, [3, 4, 5, 6])

3. Dictionary

Dictionaries store key‑value pairs and preserve insertion order in Python 3. The guide covers creation, element access, updating, deletion, and iteration methods, including legacy iterkeys / itervalues from Python 2.

In [1]: D = {}
In [2]: D[1] = 'a'
In [3]: D['a'] = 1
In [4]: D
Out[4]: {1: 'a', 'a': 1}
In [14]: D.get('a')
Out[14]: 1
In [15]: D.get('b', 2)
Out[15]: 2
In [20]: D.update(b=2, c=3)
In [21]: D
Out[21]: {'a': 1, 'b': 2, 'c': 3}
In [23]: del D['a']
In [24]: D
Out[24]: {'b': 2, 'c': 3}
In [31]: D.keys()
Out[31]: dict_keys(['b', 'c'])
In [32]: D.values()
Out[32]: dict_values([2, 3])
In [33]: D.items()
Out[33]: dict_items([('b', 2), ('c', 3)])

4. Set

Sets store unique, unordered elements. The article demonstrates creation, adding, updating, removing, set algebra (union, intersection, difference), and the immutable frozenset .

In [1]: {1, 2, 3, 1, 1}
Out[1]: {1, 2, 3}
In [2]: s = set()
In [5]: s.add(1)
In [6]: s.add(1)
In [7]: s
Out[7]: {1}
In [9]: s.update([2, 3])
In [10]: s
Out[10]: {1, 2, 3}
In [11]: s.remove(3)
In [12]: s
Out[12]: {1, 2}
In [15]: s.discard(3)  # no error if element absent
In [18]: s1 = set([1, 2, 3])
In [19]: s2 = set([3, 4, 1])
In [20]: s1 & s2
Out[20]: {1, 3}
In [21]: s1 | s2
Out[21]: {1, 2, 3, 4}
In [22]: s1 - s2
Out[22]: {2}
In [24]: fs = frozenset(['a', 'b'])
In [26]: fs.add('c')  # raises AttributeError

The article concludes with a brief disclaimer and links to additional Python resources.

pythonData StructuressetsTuplesexamplesListsDictionaries
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

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.