Example
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:
Output: complete Python program
thislist = ["apple", "banana", "cherry"]
print(thislist)
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:
Output: complete Python program
thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)
List Length
The len() function tells you how many items a list holds:
Example
Count the items and print the result:
Output: complete Python program
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
List Items - Data Types
A list can hold values of any data type:
Example
One list each of strings, integers and booleans:
Output: complete Python program
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
Example
A single list can also mix those types:
Output: complete Python program
list1 = ["abc", 34, True, 40, "male"]
type()
To Python, a list is an object whose data type is 'list':
Example
Ask Python for the type of a list:
Output: complete Python program
mylist = ["apple", "banana", "cherry"]
print(type(mylist))
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:
Output: complete Python program
thislist = list(("apple", "banana", "cherry"))
print(thislist)
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.