Python lists are meant to hold various types of data. It is composed of items inside brackets “[ ]” which are indexed and can be messed with, adding and removing items. Lists are mutable they can be altered even after their creation.
You can declare a variable list empty:
Or you can initialize it:
And you can even place different types of data in the same list:
Accessing List
Lists are indexed by integers starting on position 0 and until n-1, being n the length of the list.
Example:
Has length 5 and can be indexed like this:
0 | 1 | 2 | 3 | 4 |
Output:
Which will print out every member of List4.
But we can also access it with negative numbers which do it from the end to the start. Like this:
-5 | -4 | -3 | -2 | -1 |
Output:
This will print the List4 backwards.
Or we can also use a for to iterate over the list.
Output:
This consists of a while loop that increments a variable until it is out-of-bounds of List4.
Important: Since the list indexes start at 0, then the last index is the size of the list - 1.
We can repeat the contents by x times.
Example:
Output:
Will print the List5 which is 3 times the List4.
We can also concatenate lists by adding them.
Example:
Output:
Will print a bigger list containing List4 and List6
There is a way to finding out if a specific value exists in a specific list.
Example:
Output:
This will print out True because the string “Hey” indeed exists in List4.
Using the Membership operator, we can iterate over every member of a list.
Like this:
Output:
For each iteration of this loop, the temporary variable member will be assigned a value of the list being printed out and passing on to the next value.
For last there is a way to get the size of a specific list. Using the method len().
Example:
This code will print out the size of List4 which is 5.
Lists can be sliced to show only desired values or to iterate over desired indexes.
First-line will print from index 1 to 3 and the second will print starting on index 2.
Lists can be very useful to hold data, like student names for a runtime, trying to compare students or sort the list alphabetically. Since lists are very versatile on what data can be held, it eases a lot its use.
Function |
Description |
E.g |
len(list) |
It is used to calculate the length of the list. |
len( List7 ) will return 3 |
max(list) |
It returns the maximum element of the list. |
max( list7 ) will return 8 |
min(list) |
It returns the minimum element of the list. |
min( list7 ) will return 1 |
list(seq) |
It converts any tuple to a list. |
list((1,2,3,4,5,6,7)) will create the list [ 1, 2 , 3, 4, 5, 6, 7] |
Function |
Description |
E.g |
list.append(obj) |
The object obj is added to the list. |
List9 = ["Hello", "World"] List9.append("Smile") Will add the string “Smile” to List9 |
list.clear() |
It removes all the elements. |
List9.clear() clears the entire list List9 is now [] (an empty list) |
List.copy() |
It returns a shallow copy of the list. |
List10 = List9.copy() List10 will be a copy of List9. |
list.count(obj) |
It returns the number of occurrences of the specified object in the list. |
print(List9.count("Hello")) It will print out 1 which is the number of times the string “Hello” is in List9. |
list.extend(seq) |
The sequence represented by the object seq is extended to the list. |
List9.extend("Hey", "Smile") It extends List9 adding those two elements. |
list.index(obj) |
It returns the lowest index in the list that object appears. |
print(List9.index("Hello")) It prints out 0 which is the index of “Hello”. |
list.insert(index, obj) |
The object is inserted into the list at the specified index. |
List9.insert(1, "Hey") Inserts “Hey” right after “Hello” pushing “World” to the right. |
list.pop(obj=list[-1]) |
It removes and returns the last object of the list. |
print(List9.pop()) Will remove the string "World" and print it. |
list.remove(obj) |
It removes the specified object. |
List9.remove("World") Removes the string “World” from List9. |
list.reverse() |
It reverses the list. |
List9.reverse() Reverses the order of List9 to ["World”, “Hello”] |
list.sort([func]) |
It sorts the list by using the specified compare function if given. |
List11 = [ 3, 67, 12, 5] List11.sort() will return List11 sorted by default [ 3, 5, 12, 67] but a sorting function can be fed to the function sort. |