This article entitle Python Lists is a continuation of the previous article entitled Python String.
Lists in Python are the most versatile data type that can be used for storing values on a single variable which is separated by a comma on each item between in square brackets.
What are lists in Python?
In Python, a list is one of the built-in data types that is usually used for storing multiple items into a single variable.
Example
listOne = ['glenn', 'jude', 1996, 2011]; listTwo = [10, 20, 30, 40, 50 ]; listThree = ["x", "y", "z", "w"]
Access values in lists in Python
Accessing values in the lists is not complex we simply use the square brackets for slicing together with an index or indexes in order to get the value in that index number.
Example
listOne = ['glenn', 'jude', 1996, 2011]; listTwo = [10, 20, 30, 40, 50 ]; print ("listOne[0]: ", listOne[0]) print ("listTwo[1:5]: ", listTwo[1:5])
Output
listOne[0]: glenn listTwo[1:5]: [20, 30, 40, 50]
Update lists in Python
Modify the list with a single or multiple elements by giving a slice in the left side hand of the assignment operator and then add elements to empty lists with the append() method.
Take Note: The append() method will be discussed in the next section of this Python tutorial.
Example
listOne = ['glenn', 'jude', 1996, 2011]; print ("Value available at index 2 : ") print (listOne[2]) listOne[2] = 2001; print ("New value available at index 2 : ") print (listOne[2])
Output
Value available at index 2 : 1996 New value available at index 2 : 2001
Delete list elements in Python
To delete elements in a list you can use the del statement if you know the exact elements that you want to delete or use the remove() method if you did not know.
Take Note: The remove() method will be discussed in the next section of this Python tutorial.
Example
listOne = ['glenn', 'jude', 1996, 2011]; print (listOne) del listOne[3]; print ("After deleting value at index 3 : ") print (listOne)
Output
['glenn', 'jude', 1996, 2011] After deleting value at index 3 : ['glenn', 'jude', 1996]
Basic lists operations in Python
Python lists response to a + and * operators similar to a string which means concatenation and repetition is also available except on the results is a new print list, not a string.
Further, the list always responds to all the general sequence operations that we used on the strings in the prior chapter.
Python Expression | Results | Description |
---|---|---|
len([1, 2, 3]) | 3 | Length |
[1, 2, 3] + [4, 5, 6] | [1, 2, 3, 4, 5, 6] | Concatenation |
[‘Hi!’] * 4 | [‘Hi!’, ‘Hi!’, ‘Hi!’, ‘Hi!’] | Repetition |
3 in [1, 2, 3] | True | Membership |
for x in [1, 2, 3]: print x, | 1 2 3 | Iteration |
In-built list functions and methods in Python
The following are built-in lists function
Function | Description |
---|---|
cmp(list1, list2) | Compares elements of both lists. |
len(list) | Gives the total length of the list. |
max(list) | Returns item from the list with max value. |
min(list) | Returns item from the list with min value. |
list(seq) | Converts a tuple into list. |
The following are built-in lists methods
Methods | Description |
---|---|
list.append(obj) | Appends object obj to list |
list.count(obj) | Returns count of how many times obj occurs in list |
list.extend(seq) | Appends the contents of seq to list |
list.index(obj) | Returns the lowest index in list that obj appears |
list.insert(index, obj) | Inserts object obj into list at offset index |
list.pop(obj=list[-1]) | Removes and returns last object or obj from list |
list.remove(obj) | Removes object obj from list |
list.reverse() | Reverses objects of list in place |
list.sort([func]) | Sorts objects of list, use compare func if given |
Summary
In summary, you have read about Python Lists. We also discussed in this article what are lists in Python, access values in lists, update lists, delete list elements, basic lists operations, and In-built list functions and methods.
I hope this article about Lists in Python programming language could help you a lot to continue pursuing learning this powerful programming language.
If you want to learn more check out my previous and latest articles for more career-changing articles.