Lists Datatype in Python
Lists are Python’s most flexible ordered collection object type. It can also be referred to as a sequence that is an ordered collection of objects that can host object of any type, numbers, letters, strings and nested lists as well. It is one of the most used and versatile Python datatypes. In this module we will learn all about Lists in order to get started with it. Following is the list of all the topics that we are going to cover in this module, in case you want to jump to a specific one.
- Creating a list
- Creating Multi-Dimensional Lists
- List Comprehension
- List Extension
- Accessing a list
- Common List operations
- Python list methods and functions
Without any further ado, let’s get started.
Creating a list
A list can be created by putting the value inside the square bracket and values are separated by a comma.
Unlike strings, lists can contain any sort of object: numbers, strings, even other lists. Python lists are:
- Ordered collections of arbitrary objects
- Accessed by offset
- Arrays of object references
- Variable length, heterogeneous, arbitrarily nestable
- Of the category mutable sequence
- The elements are stored in the index basis with starting index as 0.
- A python list is enclosed between square ( [ ] ) brackets.
Creating Multi-Dimensional Lists
A list can hold other lists as well which can result in multidimensional lists. Let’s see how to create multi-dimensional list one by one.
[0, 0, 0]
print(two_dim_list)[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
List Comprehension.” It helps in constructing lists in a completely natural and easy way.
List1 = [ i for i in range(5)]
print(List1)[0, 1, 2, 3, 4]
Complicated list comprehension examples:
[‘ml’, ‘mi’, ‘md’, ‘ul’, ‘ui’, ‘ud’, ‘gl’, ‘gi’, ‘gd’]
first_letters = [ fruits for fruits in list_fruit ]
print(first_letters)[‘A’, ‘M’, ‘B’, ‘A’]
Python allows lists to resize in many ways. You can do that just by adding two or more of them.
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
two_dim = 1
[[0, 0, 1], [0, 0, 0], [0, 0, 0]]
- extend(): Alternately you can do that by using extend() method. See the following example.
L2 = [‘c’, ‘d’]
[‘a’, ‘b’, ‘c’, ‘d’]
- append(): Next, you can append a value to a list by calling the append() method. See the following example.
L2 = [‘c’, ‘d’]
[‘a’, ‘b’, ‘c’, ‘d’]
Accessing a list
Much similar to strings, we use the index number to access items in lists as shown below.
Accessing a list using Reverse indexing
To access list in reverse order we use indexing from -1,-2.. and so on. Here -1 represents the last item in the list.
Common list operations
Following is the list of some of the most frequently used Python list operations, along with their descriptions and examples
Slicing a Python List
We use slicing operation to print the list up-to a specific range. We use slice operation by including the starting index and ending index of the range that we want to print separated by colon as shown below:
Iterate a list
Iterating is quite simple in lists, we can just use a for loop to iterate, as shown below:
We can update a particular or multiple item of the list by using slice operation and then adding an element using append () method as shown below:
[1, 2, 3, 4, ‘Apple’, ‘Mango’, ‘Banana’]
[33, 1, 2, 3, 4, ‘Apple’, ‘Mango’, ‘Banana’]
[33, 1, 2, 3, 4, ‘Apple’, 29, ‘Mango’, ‘Banana’]
There are three ways of removing elements from lists. You can either use del keyword to remove a particular element, or you can use remove () method and the last way is using pop () method, as shown in the following code block.
print(list1)list1 = [1,2,3,4,5]
[1, 3, 4, 5]
Python list implements the sort() method for ordering (in both ascending and descending order) its elements in place.
Sort in ascending order:
[1, 2, 3, 4, 5, 6, 9]
Sort in descending order:
[9, 6, 5, 4, 3, 2, 1]
Python list methods and functions
Let’s understand different types of list methods and functions through the following table that contains a list of different functions with their respective descriptions.
|min(list_name)||Returns the minimum value from the list|
|max(list_name)||Returns the largest value from the list.|
|len(list_name)||Returns number of elements in a list.|
|cmp(list1,list2)||Compares the two lists.|
|list.reverse()||Reverse the list|
|list.sort||Sort the list|
|list(sequence)||Convert the sequence into the list|
|list.append(value)||Add the value into the list|
|list.remove(value)||Remove the value from the list|
With this we come to an end of this module. The next module highlights Python tuples, see you there!