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.

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.

=[value1,value2,…,value n]

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.

Example:

list1 = [1,2,3,4,5]
list2=[“hello”,”intellipaat”]

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.
One dimensional:

init_list = [0]*3
print(init_list)
output:
[0, 0, 0]

Two-dimensional List:

two_dim_list = [ [0]*3 ] *3

Three dimensional:

two_dim_list = [ [0]*3 ] *3
print(two_dim_list)[[0, 0, 0], [0, 0, 0], [0, 0, 0]]

List Comprehension

List Comprehension.” It helps in constructing lists in a completely natural and easy way.

List1 = [expression(iter) for iter in oldList if filter(iter)]
List1 = [ i for i in range(5)]
print(List1)[0, 1, 2, 3, 4]

Complicated list comprehension examples:

Example-1:

print ([a+b for a in ‘mug’ for b in ‘lid’])
[‘ml’, ‘mi’, ‘md’, ‘ul’, ‘ui’, ‘ud’, ‘gl’, ‘gi’, ‘gd’]

Example-2:

list_fruit = [“Apple”,”Mango”,”Banana”,”Avocado”]
first_letters = [ fruits[0] for fruits in list_fruit ]
print(first_letters)[‘A’, ‘M’, ‘B’, ‘A’]

List Extension

Python allows lists to resize in many ways. You can do that just by adding two or more of them.
Example:

two_dim = [[0]*3 for i in range(3)]print(two_dim)
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
two_dim[0][2] = 1
print(two_dim)
[[0, 0, 1], [0, 0, 0], [0, 0, 0]]
  • extend(): Alternately you can do that by using extend() method. See the following example.
L1 = [‘a’, ‘b’]
L2 = [‘c’, ‘d’]
L1.extend(L2)
print(L1)
[‘a’, ‘b’, ‘c’, ‘d’]
  • append(): Next, you can append a value to a list by calling the append() method. See the following example.
L1 = [‘a’, ‘b’]
L2 = [‘c’, ‘d’]
L1.extend(L2)
print(L1)
[‘a’, ‘b’, ‘c’, ‘d’]

Accessing a list

Much similar to strings, we use the index number to access items in lists as shown below.
Example:

list1 = [1,2,3,4,5]

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.

print(list1[-1])
print(list1[-3])

Output:

5
3

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:

list1[2:4]
output:
[3, 4]
list1[2:-1]
output:
[3, 4]list1[:2]
output:
[1, 2]

Iterate a list

Iterating is quite simple in lists, we can just use a for loop to iterate, as shown below:

for element in list1:
print(element)

Update/ add

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:
Example:

list1[4] = ‘number’print(list1)list1[4:7] = [“Apple”,”Mango”,”Banana”]
print(list1)list1.insert(0,33)
print(list1)list1.insert(6,29)
print(list1)

Output:

[1, 2, 3, 4, ‘number’]
[1, 2, 3, 4, ‘Apple’, ‘Mango’, ‘Banana’]
[33, 1, 2, 3, 4, ‘Apple’, ‘Mango’, ‘Banana’]
[33, 1, 2, 3, 4, ‘Apple’, 29, ‘Mango’, ‘Banana’]

Remove/Delete elements

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.

list1 = [1,2,3,4,5]del list1[2]list1 = [1,2,3,4,5]
list1.remove(4)
print(list1)list1 = [1,2,3,4,5]
print(list1.pop(1))
print(list1)

Output:

[1, 2, 4, 5][1, 2, 3, 5]
2
[1, 3, 4, 5]

Sorting list

Python list implements the sort() method for ordering (in both ascending and descending order) its elements in place.

list1.sort()

Sort in ascending order:

list1 = [1,3,2,4,5,9,6]
list1.sort()
print(list1)
output:
[1, 2, 3, 4, 5, 6, 9]

Sort in descending order:

list1 = [1,3,2,4,5,9,6]
list1.sort(reverse=True)
print(list1)
output:
[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.

MethodDescription
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.sortSort 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!

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
21 + 25 =