HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to Python: Print and RepeatVariablesData Types & NumbersStringsListsConditions (if / elif / else)Loops (for / while)FunctionsOperatorsDictionariesTuples & SetsError Handling (try / except)Classes (Object-Oriented)File OperationsComments & IndentationBooleans (True / False)Lambda & map / filterModules (import)InheritanceVariable Scope
REFERENCE
📖 Overview
🖥️ Output & Input
🔢 Numbers & Math
✍️ String
📋 List
items []append()insert()remove() / pop()sort() / sorted()reverse()slicing [a:b]
🗂️ Dictionary & Set
🔁 Condition & Loop
🧩 Function
📁 File & Error
🔄 Type & Conversion
Python›REFERENCE›List
📋

List

Working with ordered, mutable collections of data.

CommandDescription
items []Creates an ordered collection of values.
append()Adds an element to the end of the list.
insert()Adds an element to a specific position.
remove() / pop()Removes an element (by value or by position).
sort() / sorted()Sorts the list.
reverse()Reverses the list.
slicing [a:b]Takes a piece of the list/string.

items []

Try it yourself →

Creates an ordered collection of values.

Syntax

name = [element1, element2, ...]

Description

A list is created with square brackets. Elements are accessed with an index starting at 0; a negative index counts from the end.

Returns

A list object.

Example

colors = ["red", "purple", "green"]
print(colors[0])
print(colors[-1])

Expected output

red
green

When to use?

Holding multiple values (names, scores, products) in a single variable.

Common mistakes

  • ⚠️ Indexing starts from 0, not 1: the first element is items[0].

append()

Try it yourself →

Adds an element to the end of the list.

Syntax

items.append(element)

Description

Adds the given element to the end of the list. Modifies the list in-place, does not return new items.

Returns

None (it changes the list).

Example

basket = ["apple"]
basket.append("pear")
print(basket)

Expected output

['apple', 'pear']

When to use?

Filling items with a loop, lists that grow as the user adds elements.

Common mistakes

  • ⚠️ basket = basket.append(x) WRONG — append returns None, you will delete the list.

insert()

Try it yourself →

Adds an element to a specific position.

Syntax

items.insert(position, element)

Description

Places the element at the given index, shifting the following ones to the right.

Example

a = [1, 2, 4]
a.insert(2, 3)
print(a)

Expected output

[1, 2, 3, 4]

When to use?

Inserting an element into the middle of an ordered list.

remove() / pop()

Try it yourself →

Removes an element (by value or by position).

Syntax

items.remove(value) · items.pop(index)

Description

remove() deletes the given VALUE (the first match). pop() deletes the element AT the given POSITION and returns it; if no index is given, it takes the last one.

Returns

pop() returns the removed element.

Example

a = ["x", "y", "z"]
a.remove("y")
son = a.pop()
print(a)
print(son)

Expected output

['x']
z

When to use?

Taking a product out of a cart, taking an element from a queue.

Common mistakes

  • ⚠️ remove() throws an error for a value that doesn't exist; check 'value in items' first.

sort() / sorted()

Try it yourself →

Sorts the list.

Syntax

items.sort() · sorted(arr)

Description

sort() sorts the list IN-PLACE (original changes). sorted() returns NEW sorted items, preserving the original. Sorts in reverse with reverse=True.

Returns

sort() None; sorted() new items.

Example

a = [3, 1, 2]
a.sort()
print(a)
print(sorted([5, 4], reverse=True))

Expected output

[1, 2, 3]
[5, 4]

When to use?

Scoreboard, alphabetical items, sorting from largest to smallest.

reverse()

Try it yourself →

Reverses the list.

Syntax

items.reverse()

Description

Reverses the order of the elements (in place). It doesn't sort, it just reverses.

Example

a = [1, 2, 3]
a.reverse()
print(a)

Expected output

[3, 2, 1]

When to use?

Bringing the last-added one to the front, iterating in reverse order.

slicing [a:b]

Try it yourself →

Takes a piece of the list/string.

Syntax

items[start:end:step]

Description

You take a subset with square brackets. Start is included, end is excluded. A negative index counts from the end, and the step specifies skipping.

Returns

New items/text (slice).

Example

h = "HexLibrary"
print(h[0:3])
print(h[-3:])
print([0,1,2,3,4][::2])

Expected output

Hex
ary
[0, 2, 4]

When to use?

Taking the first N characters, cutting off the last part, selecting a piece of a list.

Common mistakes

  • ⚠️ The end index is not included: h[0:3] takes 0,1,2, but not 3.
HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code