Skip to main content

Example

main.py
mylist = ["apple", "banana", "cherry"]

List

A list lets one variable hold several items at once.

Python has 4 built-in types for storing collections of data. Lists are one of them; Tuple, Set and Dictionary are the others, and each has its own qualities and uses.

To create a list, put the items between square brackets:

Example

Building a simple list:

Prompt
Create a Python list with the items "apple", "banana", and "cherry". Print the list.
Output: complete Python program

List Items

Three things define list items: they keep their order, they can be changed, and the same value may appear more than once.

Each item also has an index. Counting starts at zero, so index [0] is the first item, index [1] is the next one, and so on.


Ordered

Being ordered means a list keeps its items in a set sequence, and that sequence stays put.

Anything new you add goes after the items already there.

Note: A few list methods do rearrange the items, but apart from those, the sequence stays as it is.


Changeable

You are free to modify a list once it exists: items can be changed, added and removed at any time.


Allow Duplicates

Because every item has its own index, two items can share a value without any conflict:

Example

Here "apple" and "cherry" each appear twice:

Prompt
Create a Python list that contains duplicate values: "apple", "banana", "cherry", "apple", "cherry". Print the list to show that duplicates are allowed.
Output: complete Python program

List Length

The len() function tells you how many items a list holds:

Example

Count the items and print the result:

Prompt
Create a list with three fruits and use the len() function to print how many items are in the list.
Output: complete Python program

List Items - Data Types

A list can hold values of any data type:

Example

One list each of strings, integers and booleans:

Prompt
Create three lists: one with strings, one with integers, and one with boolean values. Do not print the lists.
Output: complete Python program

Example

A single list can also mix those types:

Prompt
Create a list that contains different data types: a string, an integer, a boolean, another integer, and another string.
Output: complete Python program

type()

To Python, a list is an object whose data type is 'list':

Example

Ask Python for the type of a list:

Prompt
Create a list and use the type() function to print the data type of the list.
Output: complete Python program

The list() Constructor

You can also build a new list by calling the list() constructor.

Example

Passing a tuple to list() gives us a list:

Prompt
Use the list() constructor to create a list with "apple", "banana", and "cherry". Print the list.
Output: complete Python program

Python Collections (Arrays)

Python gives you four collection data types, and each one behaves a little differently:

  • List keeps its order and can be changed. Duplicates are fine.
  • Tuple keeps its order but cannot be changed. Duplicates are fine.
  • Set has no order and no indexes, and is unchangeable*. Duplicates are not allowed.
  • Dictionary keeps its order** and can be changed. Duplicates are not allowed.

*You can't change the items of a set once they are in it, but you are free to add new ones or take existing ones out at any time.

**Dictionaries are ordered from Python 3.7 onward. In Python 3.6 and earlier they are unordered.

It pays to know these properties before you pick a type for your data. The right choice can keep the meaning of the data intact, and it can also make your program more efficient or more secure.