Python comes with a wonderful list type called “list”. List literals are written in square brackets []. Lists function in the same way as strings. Use the len() function with square brackets [] to access data. The index zero element is the first.
Lists can be used to store multiple items within a single variable.
Lists are one among the 4 data types that Python has built in to store data collections. The other 3 are Tuple and Set. Each type has different properties and uses.
Square brackets are used to create lists:
thislist = ["Python", "Java", "PHP"]print(thislist)
The list items can be ordered, changed, and allowed to have duplicate values.
Indexing is done for list items. The index [0]
first, and the index [1]
second, respectively.
If we say that lists can be ordered, it means that items are in a predetermined order and will not change.
You can add new items to your list by placing them at the end.
Variable
A list can be modified after it is created. This means that items can be added, removed, or changed.
Lists can contain items of the same value because they are indexed
thislist = ["Python", "Java", "PHP", "Java", "Python"]print(thislist)
Use the len()
function to determine how many items are in a list.
thislist = ["Python", "Java", "PHP"]print(len(thislist))
Any data type can be used to create list items:
list1 = ["Python", "Java", "PHP"]list2 = [1, 5, 7, 9, 3]list3 = [True, False, False]
Python sees lists as objects that have the data type “list”:
<class 'list'>
mylist = ["Python", "Java", "PHP"]print(type(mylist))
Quick Links
Legal Stuff
Social Media