Fundamentals 8 min read

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

This article introduces Python’s fundamental data structures—lists, tuples, sets, and dictionaries—detailing how to create them, use common methods, perform operations like concatenation, and understand their mutability with clear code examples.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master Python’s Core Data Structures: Lists, Tuples, Sets, and Dictionaries Explained

Python provides several built‑in data structures that are essential for everyday programming.

List

Lists are ordered, mutable collections defined with square brackets. Elements can be of any type and duplicate values are allowed.

In [1]: list1 = ['wellcom','to','the','sjwjyaisf1688']
Out[1]: ['wellcom', 'to', 'the', 'sjwjyaisf1688']
In [2]: list2 = ['wellcom','to','the','sjwjyaisf1688',6,6,6]
Out[2]: ['wellcom', 'to', 'the', 'sjwjyaisf1688', 6, 6, 6]
# create with list()
In [3]: list(['xiao','xiao','wa','jue','ji',666])
Out[3]: ['xiao', 'xiao', 'wa', 'jue', 'ji', 666]
In [4]: list('666')
Out[4]: ['6', '6', '6']
# concatenation
In [5]: ['wellcom','to','te'] + ['xiao','xiao','ji','666']
Out[5]: ['wellcom', 'to', 'te', 'xiao', 'xiao', 'ji', '666']
# append
In [6]: list2 = ['a','b','c']
In [7]: list2.append('d')
Out[7]: ['a', 'b', 'c', 'd']
# extend
In [8]: list2.extend(['e','f'])
Out[8]: ['a', 'b', 'c', 'd', 'e', 'f']

Tuple

Tuples are ordered, immutable collections. They are defined by commas, optionally enclosed in parentheses.

In [1]: tuple1 = 1,2,3
In [2]: tuple2 = "sjwjyaisf1688","xiaoxiaowajueji666"
In [3]: tuple3 = (1,2,3,4)
In [4]: tuple4 = ()
In [5]: tuple5 = (1,)
In [6]: print(tuple1,tuple2,tuple3,tuple4,tuple5)
Out: (1, 2, 3) ('sjwjyaisf1688', 'xiaoxiaowajueji666') (1, 2, 3, 4) () (1,)
# concatenation
In [7]: (1,2,3) + (4,5,6)
Out[7]: (1, 2, 3, 4, 5, 6)

Attempting to assign to a tuple element raises a TypeError because tuples are immutable.

Set

Sets are unordered collections of unique elements, created with curly braces or the set() constructor.

In [1]: drink = {'water','milk','lemonade','beer','sprite'}
Out[1]: {'beer', 'lemonade', 'milk', 'sprite', 'water'}
In [2]: drink = set(['water','milk','lemonade','beer','sprite','milk'])
Out[2]: {'beer', 'lemonade', 'milk', 'sprite', 'water'}
A = {1,2,3,4,5,6}
B = {3,4,5}
# difference
In [3]: A - B
Out[3]: {1, 2, 6}
# union
In [4]: A | B
Out[4]: {1, 2, 3, 4, 5, 6}
# intersection
In [5]: A & B
Out[5]: {3, 4, 5}

Dictionary

Dictionaries store key‑value pairs and provide fast lookup. Keys must be unique.

In [1]: dict1 = {'xiaoming':24,'xiaofang':28,'zhangsan':21,'wangwu':27}
Out[1]: {'wangwu': 27, 'xiaofang': 28, 'xiaoming': 24, 'zhangsan': 21}
In [2]: dict1.keys()
Out[2]: dict_keys(['xiaoming','xiaofang','zhangsan','wangwu'])
In [3]: dict1.values()
Out[3]: dict_values([24,28,21,27])
In [4]: dict1['xiaoming']
Out[4]: 24
# duplicate key overwrites previous value
In [5]: dict2 = {'xiaoming':24,'xiaofang':28,'zhangsan':21,'wangwu':27,'xiaoming':25}
Out[5]: {'xiaoming': 25, 'xiaofang': 28, 'zhangsan': 21, 'wangwu': 27}
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.

PythonData StructuresTutorialListdictionarytupleSet
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

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.