Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Learn/Python Overview/Part 3 · 3. Data Structures
Lesson 5 of 17

Lists

Probably the most fundamental data structure in Python is the list.

Derpx061 Jan · 1 min read
Python Overview
ListsTuplesDictionariesSets
Contents
IndexingSlicingMembershipConcatenationUnpacking
0% read · 1 min leftBack to top ↑
Course outline & contents0%
Python Overview
ListsTuplesDictionariesSets
Contents
IndexingSlicingMembershipConcatenationUnpacking

Probably the most fundamental data structure in Python is the list. A list is simply an ordered collection. (It is similar to what in other languages might be called an array, but with some added functionality.)

basics.py
integer_list = [1, 2, 3]
heterogeneous_list = ["string", 0.1, True]
list_of_lists = [ integer_list, heterogeneous_list, [] ]

list_length = len(integer_list)   # equals 3
list_sum = sum(integer_list)      # equals 6

Indexing

You can get or set the nth element of a list with square brackets:

indexing.py
x = range(10)   # is the list [0, 1, ..., 9]
zero = x[0]     # equals 0, lists are 0-indexed
one = x[1]      # equals 1
nine = x[-1]    # equals 9, 'Pythonic' for last element
eight = x[-2]   # equals 8, 'Pythonic' for next-to-last element

x[0] = -1       # now x is [-1, 1, 2, 3, ..., 9]

Slicing

You can also use square brackets to "slice" lists:

slicing.py
first_three = x[:3]                 # [-1, 1, 2]
three_to_end = x[3:]                # [3, 4, ..., 9]
one_to_four = x[1:5]                # [1, 2, 3, 4]
last_three = x[-3:]                 # [7, 8, 9]
without_first_and_last = x[1:-1]    # [1, 2, ..., 8]
copy_of_x = x[:]                    # [-1, 1, 2, ..., 9]

Membership

Python has an in operator to check for list membership:

membership.py
1 in [1, 2, 3]    # True
0 in [1, 2, 3]    # False

[!WARNING] This check involves examining the elements of the list one at a time, which means that you probably shouldn’t use it unless you know your list is pretty small (or unless you don’t care how long the check takes).

Concatenation

It is easy to concatenate lists together:

extend.py
x = [1, 2, 3]
x.extend([4, 5, 6])     # x is now [1,2,3,4,5,6]

If you don’t want to modify x you can use list addition:

addition.py
x = [1, 2, 3]
y = x + [4, 5, 6]       # y is [1, 2, 3, 4, 5, 6]; x is unchanged

More frequently we will append to lists one item at a time:

append.py
x = [1, 2, 3]
x.append(0)      # x is now [1, 2, 3, 0]
y = x[-1]        # equals 0
z = len(x)       # equals 4

Unpacking

It is often convenient to unpack lists if you know how many elements they contain:

unpacking.py
x, y = [1, 2]    # now x is 1, y is 2

although you will get a ValueError if you don’t have the same numbers of elements on both sides.

It’s common to use an underscore for a value you’re going to throw away:

underscore.py
_, y = [1, 2]    # now y == 2, didn't care about the first element
Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonLogic & ArithmeticNext lesson →Tuples