DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

Python Lists: How to Create, Change, and Use Lists in Python

Python lists are mutable sequences for storing ordered items. Learn how to create, access, update, sort, and remove list elements with runnable examples.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A Python list stores an ordered collection of items that you can read, change, add to, and remove from. Use square brackets to create one: colors = ["red", "blue"]. List positions start at index 0, so colors[0] is "red".

What is a Python list?

A list is a mutable sequence: it keeps items in order, and you can change its contents after creating it. Lists support indexing and slicing, as well as iteration. The Python 3.14.7 data structures tutorial explains these sequence operations alongside related sequence types.

As an Amazon Associate I earn from qualifying purchases.

colors = ["red", "blue", "green"]
print(colors[1])    # blue
print(colors[0:2])  # ['red', 'blue']

colors[1] = "yellow"
print(colors)       # ['red', 'yellow', 'green']

Use a for loop to process each item in order:

for color in colors:
    print(color)

How do you add items to a list?

append() adds one item to the end. extend() adds each item from an iterable. The distinction matters when the item you want to add is itself a list.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
numbers = [1, 2]
numbers.append([3, 4])
print(numbers)  # [1, 2, [3, 4]]

numbers = [1, 2]
numbers.extend([3, 4])
print(numbers)  # [1, 2, 3, 4]

To add an item before a particular position, use insert(index, item):

colors = ["red", "blue"]
colors.insert(1, "yellow")
print(colors)  # ['red', 'yellow', 'blue']

How do you remove items?

Choose the removal operation based on whether you have a value, a position, or a range—and whether you need the removed item back.

Operation How it selects items What it does
remove(value) First item equal to the value Removes it; raises ValueError if no matching item exists
pop([index]) Item at the given index, or the last item if the index is omitted Removes and returns the item
del list[index] or del list[start:stop] Index or slice Deletes the selected item or items without returning them
items = ["pen", "book", "pen"]
items.remove("pen")
print(items)  # ['book', 'pen']

last_item = items.pop()
print(last_item)  # pen
print(items)      # ['book']

del items[0]

remove() removes only the first equal value. To remove every matching value, build a new list containing only the items you want to keep; a comprehension is shown below.

Which list operations change the list?

Methods such as append(), insert(), remove(), reverse(), and sort() modify the existing list. Modifying list methods return None, not the changed list, so do not assign their result back to the list variable.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
scores = [7, 2, 9]
scores.sort()
print(scores)  # [2, 7, 9]

scores = [7, 2, 9]
result = scores.sort()
print(result)  # None

Use sorted() when you want a sorted result while keeping the original list unchanged:

scores = [7, 2, 9]
ordered = sorted(scores)
print(ordered)  # [2, 7, 9]
print(scores)   # [7, 2, 9]

copy() also returns a result: a shallow copy of the list. It creates a separate outer list, but nested mutable objects are not recursively copied.

Can a list work as a stack or queue?

Stack: add and remove at the end

A list is a natural fit for a stack, where the most recently added item is removed first. Use append() to put an item on top and pop() to take the last item off.

stack = ["first"]
stack.append("second")
top = stack.pop()
print(top)    # second
print(stack)  # ['first']

Queue: use a deque for both ends

A list can hold queue items, but removing from its front requires the remaining elements to shift. For a queue that adds and removes at both ends, use collections.deque, which is designed for fast operations at either end.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from collections import deque

queue = deque(["first", "second"])
queue.append("third")
next_item = queue.popleft()
print(next_item)  # first
print(queue)      # deque(['second', 'third'])
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How do list comprehensions work?

A list comprehension constructs a new list by transforming each input item and, optionally, selecting items with a condition. Start with a regular loop:

squares = []
for number in range(5):
    squares.append(number * number)

The equivalent comprehension is shorter and still creates a new list:

squares = [number * number for number in range(5)]

Add a condition to include only selected items:

even_squares = [number * number for number in range(5) if number % 2 == 0]
print(even_squares)  # [0, 4, 16]

The Python tutorial’s list-comprehension section covers this construction form and further examples.

Quick reference: common list methods

  • append(item): add one item at the end.
  • extend(iterable): add the iterable’s items at the end.
  • insert(index, item): insert before the specified position.
  • remove(value): remove the first equal value.
  • pop([index]): remove and return an indexed item, or the last item by default.
  • sort(): sort the existing list in place.
  • reverse(): reverse the existing list in place.
  • copy(): return a shallow copy.

For the full behavior and additional list operations, see the Python 3.14.7 data structures tutorial.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.