A list is a collection of objects
Everything in Python is an object. These objects in a list are numbers in most cases. Lists can also contain text (strings) or both.
Related Course: Complete Python Programming Course & Exercises
Python Lists
A list is a collection of objects. This is something like an array in other languages. A list is enclosed by brackets []
and each object is separated by a comma, as shown below:
'meow','bar','woof'] a = [ |
Important features of a list are:
- Lists are ordered
- List can contain arbitrary objects
- List element can be accessed by index
- List are mutable
- Lists are dynamic
Lists are ordered
A list is an ordered collection of objects. The order in which you add elements to your list is stored.
'meow','bark','woof'] a = [ |
If you compare two lists (==
) and the order is not the same, then the lists are not equal.
1,2,3] == [3,2,1] [ |
Only if the list contents is exactly the same, the lists are equal.
'meow','bark','woof'] a = [ |
Related Course: Complete Python Programming Course & Exercises
List can contain arbitrary objects
A list can contain any type ofobject. A list can contain numeric elements:
1,3,6,9] a = [ |
It can be strings:
"meow","bark","woof"] a = [ |
A combination of types if fine too:
1,"meow",2,3,"bark"] a = [ |
A list can have any number of elements. It can be empty []
or have many objects.
a = [] |
The items in a list don’t have to be unique.
'meow','bark','meow','woof'] a = [ |
List elements can be accessed by index
Individual elements in the list can be accesses by an index. The index starts counting from zero, where [0]
is the first element in the list.
If this is your list:
'meow','woof','bark','cheep'] a = [ |
Then you can access individual elements using the index:
0] a[ |
To print the last element, you can count from the back using the minus sign.print(a[-1])
cheep |
You can do slicing on lists, the same way as you would slice strings.
The example below slices the list twice:
'A','B','C','D','E','F'] a = [ |
If first index is zero, it can be omitted:
2] a[: |
You can check if a list contains an element, by using the in
keyword:
'meow','woof','bark','cheep'] a = [ |
Lists are mutable
A list is a mutable data type. After creation of a list, the list can be changed. Elements can be added, modified, deleted or moved around.
'A','B'] a = [ |
Lists are dynamic
A list is dynamic. They can grow or shrink in size during the execution of your program.
You can grow a list like this:
a = [] |
Or using the append()
function:
a = [] |
The list can shrink, if elements are deleted:
del a[2] |
Python list get size
You can get the length of the list using the len() function. This function can be applied to any list. It will return the number of elements in the list.
Example code:
a = [] |
'meow','woof','bark','cheep'] a = [ |
Python list remove
By calling the remove() method, you can remove an element. This method is called on the list object.
'meow','woof','bark','cheep'] a = [ |
Python list append in front
The method append()
adds an element to the end of the list. What if you want to append to the front?
You can use array.insert(index, value)
, like so:
'meow','woof','bark','cheep'] a = [ |
You can also do this:
'meow','woof','bark','cheep'] a = [ |
Python list pop
The pop()
method removes the last object from the list. The method returns the removed object.
1,2,3,4,5] x = [ |
Python list sort
You can sort a list by calling the .sort()
method. Call this method on the list itself. The method sorts from low to high.
6,3,4,2,5,1] a = [ |
If you want in reverse order, just add the parameter reverse=True
.
6,3,4,2,5,1] a = [ |
Python list count
If you want to know how many times an object occurs inside a list, you can use the count()
method.
The example below counts the frequency of some items:
6,5,4,1,2,4,3] a = [ |
This works for any type of data in the list:
'meow','foo','meow','meow'] a = [ |
If you are a Python beginner, then I highly recommend this book.
If I make a list which contains other lists, then how can I print elements of child-list.
I am trying to use it as multidimensional list.
Use an index to print the child list: print(l[3])
how to accept a list of numbers as input
You can use:
Example input: