English
/

Python Lists for Beginners, Create, Index, Slice, and Change

Last edited Oct 29, 2026 · 4 min read

Create a Python list with square brackets, such as tasks = ["email Dana", "fix report", "book room"], and read its first item with tasks[0]; indexes start at 0 and tasks[-1] reads the last item. Lists can be sliced and changed in place. Assignment shares the same list, while copy() creates a shallow copy.

On this page▾
  1. How do you create a list in Python and read an index?
  2. How does indexing a Python list differ from slicing?
  3. Changing a list
  4. Checking what is in a list
  5. Mistake 1: saving the result of sort()
  6. Mistake 2: thinking = makes a copy
  7. Frequently asked questions
  8. How do I create a list in Python?
  9. What does an index mean in a Python list?
  10. What is the difference between list indexing and slicing in Python?

How do you create a list in Python and read an index?

A list is a sequence of values inside square brackets, separated by commas.

tasks = ["email Dana", "fix report", "book room"]
print(tasks[0])
print(tasks[-1])
print(len(tasks))

Output:

email Dana
book room
3

Positions, called indexes, start at 0, so tasks[0] is the first item. Negative indexes count from the end, so tasks[-1] is the last item. The official tutorial explains this for strings, and lists follow the same rules: the Data Structures chapter notes that lists and strings share properties such as indexing and slicing.

len() tells you how many items the list holds.

How does indexing a Python list differ from slicing?

A slice uses a colon to grab a range of items. The start position is included and the end position is not.

scores = [72, 85, 90, 64, 88]
print(scores[1:3])
print(scores[:2])
print(scores[-2:])
print(scores[1:100])

Output:

[85, 90]
[72, 85]
[64, 88]
[85, 90, 64, 88]

scores[1:3] gives positions 1 and 2. Leaving out the start means "from the beginning," and leaving out the end means "to the end." A slice that runs past the end stops at the last item rather than raising an error. Asking for a single position that does not exist is different. scores[5] raises IndexError: list index out of range.

Changing a list

Unlike strings, lists can be changed in place. You can replace an item by assigning to its index:

scores[0] = 75
print(scores)

Output:

[75, 85, 90, 64, 88]

The Data Structures chapter documents these list methods:

Method What it does
append(x) Adds an item to the end
insert(i, x) Inserts an item before position i
remove(x) Removes the first item equal to x
pop() Removes and returns the last item
count(x) Counts how many times x appears
index(x) Returns the position of the first x
sort() Sorts the list in place

Here they are working on the task list from earlier:

tasks.append("call vendor")
tasks.insert(0, "check calendar")
print(tasks)
tasks.remove("fix report")
finished = tasks.pop()
print(finished)
print(tasks)

Output:

['check calendar', 'email Dana', 'fix report', 'book room', 'call vendor']
call vendor
['check calendar', 'email Dana', 'book room']

insert(0, ...) put the new task at the front. pop() both removed the last task and handed it back, so it could be stored in finished.

Among these methods, remove(), pop(), and index() can raise errors. The tutorial documents them. remove() raises a ValueError if the item is not in the list, and pop() raises an IndexError if the list is empty. Running them confirms the messages: ValueError: list.remove(x): x not in list and IndexError: pop from empty list.

Checking what is in a list

The in keyword answers "is this value in the list?" with True or False.

print(85 in scores, 100 in scores)
print(scores.count(85), scores.index(90))

Output:

True False
1 2

Going through a list item by item with a loop is covered in Python Loops, For vs While, With Examples You Can Run.

Mistake 1: saving the result of sort()

There are two ways to sort. sorted(scores) returns a new, sorted list and leaves the original alone. scores.sort() sorts the original list itself and returns None.

print(sorted(scores))
print(scores)
result = scores.sort()
print(result)
print(scores)

Output:

[64, 75, 85, 88, 90]
[75, 85, 90, 64, 88]
None
[64, 75, 85, 88, 90]

The official tutorial explains why result is None: methods like insert, remove and sort that only modify the list return the default None, and the tutorial calls this a design principle for all mutable data structures in Python. So scores = scores.sort() is a classic bug: it sorts the list and then replaces it with None. Use scores.sort() on its own line, or scores = sorted(scores).

Mistake 2: thinking = makes a copy

Assignment does not copy a list.

a = ["x", "y"]
b = a
b.append("z")
print(a)

Output:

['x', 'y', 'z']

You only changed b, but a changed too. That is because b = a does not create a second list. It gives the same list a second name. When you need an independent copy, use the copy() method, which the tutorial describes as returning a shallow copy of the list:

c = a.copy()
c.append("w")
print(a, c)

Output:

['x', 'y', 'z'] ['x', 'y', 'z', 'w']

Appending to c leaves a alone. A shallow copy still shares any nested mutable objects, so changing a nested list can affect both copies.

Frequently asked questions

How do I create a list in Python?

Put comma-separated values in square brackets: items = ["a", "b", "c"]. Use [] for an empty list, and append() to add an item at the end.

What does an index mean in a Python list?

It identifies an item’s zero-based position. For a nonempty list, items[0] is the first item and items[-1] is the last.

What is the difference between list indexing and slicing in Python?

Indexing selects one item and can raise IndexError for an invalid position. A slice selects a range, includes its start and excludes its end; an end beyond the list is truncated rather than raising an out-of-range error.

Sources

  1. Python Software Foundation, Built-in Types, Lists
  2. Python Software Foundation, The Python Tutorial, Data Structures
  3. Python Software Foundation, The Python Tutorial, An Informal Introduction to Python

About this guide

Howlane operator · Checked against the official sources listed above. It is general information, not professional advice.

AboutEditorial standardsCorrectionsReport an error

Edit history

  • Updated
  • Updated
  • Updated
  • Updated
  • Published