Fundamentals 14 min read

Master Python Lists, Dictionaries, Tuples, and Sets: Essential Operations

This guide explains Python's core collection types—lists, dictionaries, tuples, and sets—detailing their characteristics, creation methods, and common operations such as indexing, slicing, merging, sorting, and set algebra, with clear code examples for each.

Ops Development Stories
Ops Development Stories
Ops Development Stories
Master Python Lists, Dictionaries, Tuples, and Sets: Essential Operations

List

In Python, a list is an ordered, mutable collection that can hold any type of object, including other lists, dictionaries, or tuples.

Ordered collection accessed by index

Supports heterogeneous elements and nesting

Mutable: items can be added, removed, or changed in place

Common List Operations

Concatenation: list1 + list2 joins two lists

Repetition: list * 3 repeats the list three times

Iteration: for i in list: print(i) Membership test: 3 in list Index lookup: list.index(1) Count occurrences: list.count(1) Slicing: list[1:3] creates a new sub‑list

Length:

len(list)

Basic List Creation

>> list=[]
>>> list=[1, 2, '3', []]
>>> list
[1, 2, '3', []]

Indexing and Slicing

>> list[1]
2
>>> list[0:3]
[1, 2, '3']

Repeating a List

>> list*3
[1, 2, '3', [], 1, 2, '3', [], 1, 2, '3', []]

In‑place Modification

>> food=['spam', 'eggs', 'milk']
>>> food[1] = 'Eggs'
>>> food
['spam', 'Eggs', 'milk']

List Methods

Append: food.append('cake') Sort: food.sort() Extend (merge): list1.extend(list2) Pop: list1.pop() Reverse: list1.reverse() Insert: list.insert(2, 10) Delete: del list[2] Index:

list.index(3)

Dictionary

A dictionary is an unordered collection of key‑value pairs where keys must be immutable and unique.

Access by key rather than position

Mutable: values can be added, updated, or removed

Keys can be any immutable type (strings, numbers, tuples)

Basic Dictionary Operations

>> dict={'a':97,'b':98}
>>> len(dict)
2
>>> print("ascii code of 'a' is {}, ascii code of 'b' is {}".format(dict['a'], dict['b']))
ascii code of 'a' is 97, ascii code of 'b' is 98

Key Presence

>> 'a' in dict
True
>>> dict.get('c', 'none')
'none'

Updating Values

>> food={'eggs':3,'ham':1,'spam':4}
>>> food['ham'] = 2
>>> food['branch'] = ['bacon', 'bake']
>>> del food['eggs']
>>> food
{'ham': 2, 'spam': 4, 'branch': ['bacon', 'bake']}

Dictionary Methods

get: safe retrieval with default

pop: remove and return a value

clear: empty the dictionary

update: merge another mapping

Tuple

A tuple is an ordered, immutable collection. Because it cannot be changed, it is often used for fixed‑size groups of items.

Ordered and indexable

Immutable: no in‑place modifications

Can contain heterogeneous elements and be nested

Creating Tuples

>> tuple=()
>>> tuple=(1,)
>>> type(tuple)
<class 'tuple'>
>>> tuple=(1,2,'3',(4,5))
>>> tuple
(1, 2, '3', (4, 5))

Conversion from List

>> lst=[1,2,3,4]
>>> tup=tuple(lst)
>>> tup
(1, 2, 3, 4)

Tuple Operations

Concatenation: (1,2)+(3,4) Repetition: (1,2)*3 Index lookup: tup.index(3) Count: tup.count(3) Slicing works like lists

Set

A set is an unordered collection of unique, hashable elements. Sets support mathematical operations such as union, intersection, and difference.

Unordered, no duplicate elements

Elements must be immutable

Useful for membership testing and eliminating duplicates

Creating Sets

>> s=set('a')
>>> a=set({'k1':1,'k2':2})
>>> c={'a','b','c'}
>>> d=('a','b','c')  # tuple, not a set

Set Operations

Difference: a.difference(b) Intersection: a.intersection(b) Union: a.union(b) Symmetric difference: a.symmetric_difference(b) Subset / Superset checks: a.issubset(b), a.issuperset(b) Update (in‑place union): a.update(b) Discard / Remove / Pop for element deletion

>> a={1,2,3,4}
>>> b={3,4,5,6}
>>> a.intersection(b)
{3, 4}
>>> a.union(b)
{1, 2, 3, 4, 5, 6}
>>> a.difference(b)
{1, 2}
>>> a.symmetric_difference(b)
{1, 2, 5, 6}
>>> a.update(b)
>>> a
{1, 2, 3, 4, 5, 6}

Conversion

>> a=set(range(5))
>>> li=list(a)
>>> tup=tuple(a)
>>> st=str(a)
>>> print(li)
[0, 1, 2, 3, 4]
>>> print(tup)
(0, 1, 2, 3, 4)
>>> print(st)
{0, 1, 2, 3, 4}
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.

PythonListdictionarybasicstupleSet
Ops Development Stories
Written by

Ops Development Stories

Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.

0 followers
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.