Knowledgebase

Lists and Sequences Print

  • python, guide, howto, solution, zillionkinghost, hosting, support
  • 0

Ordered collections.

WHAT A LIST IS

An ordered collection you can change.

WHAT YOU DO WITH THEM

Add items Remove items Access by position Iterate over them Sort them Check membership

POSITIONS START AT ZERO

The first item is at position zero.

The most common source of errors for beginners.

NEGATIVE POSITIONS

Count from the end, which is frequently convenient.

SLICING

Taking a portion of a list by range.

Extremely useful and worth learning properly.

WHAT TO BE CAREFUL WITH

Modifying a list while iterating over it, which produces surprising results Assuming a list is sorted Very large lists held entirely in memory

WHAT TO USE INSTEAD OF A LIST

A set, when you only check membership and uniqueness matters A dictionary, when you look items up by key

WHY

Both are far faster for those purposes on large collections.

WHAT TUPLES ARE FOR

Collections that must not change, and as dictionary keys.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot