Saturday, February 9, 2019

Python Lists (Arrays)

In Python, Lists are like Arrays in other programming languages. List is a collection of data and is written in square brackets. Lists are ordered collection of data, which means it has an index for every element. Index of a list starts with 0. Lists in Python are mutable i.e. they can be modified.

Creating an empty list:

We can create an empty list as following

someList = []

We can also use the list() function to create an empty list

someList = list()

Creating lists containing values:

To create a non-empty lists,

colorList = ["red", "green", "yellow", "blue", "orange"]
numberList = [25, 36, 45, 12, 9]

Creating a list using for loop:

This is strange but powerful! We can create lists in Python using for loops in the way shown below.

numList = [x for x in range(1, 10)]
print(numList)

The above code will print [1, 2, 3, 4, 5, 6, 7, 8, 9].

Python Lists can hold mixed type of data:

A list in python can hold elements of different data types, even another list as an element. If a list contains another list as an element it is also called a nested list. Below is an example of such a list.

mixedList = [24, 'Apple', 34.5, [5, 6, 7], 4+3j]

Here we can see that 24 is an integer, 'Apple' is a string, 34.5 is float, [5, 6, 7] is a list and 4+3j is a complex number.

Accessing items in a list:

The following code will print the third item, yellow, in the list as the index of a list starts from 0.

colorList = ["red","green","yellow","blue","orange"]
print(colorList[2])

Modifying items in a list:

The following code will change the third item from yellow to white and then print the modified list.

colorList = ["red","green","yellow","blue","orange"]
colorList[2] = "white"
print(colorList)

Checking if a list contains an item:

This will check if colorList contains blue.

colorList = ["red","green","yellow","blue","orange"]
if 'blue' in colorList:
    print('The list contains the color blue')

Joining lists:

We can use the + operator to join lists.

list1 = ["red","green","yellow"]
list2 = ["blue","orange"]
list3 = list1 + list2
print(list3)

We can also use the extend() method to join lists but differently.

list1 = ["red","green","yellow"]
list2 = ["blue","orange"]
list1.extend(list2)
print(list1)
print(list2)

After execution of the above code, list1 will become ['red', 'green', 'yellow', 'blue', 'orange'] but list2 will remain same ['blue', 'orange'].

Slicing lists:

We can extract a sub-list from a list by specifying the start and end index, as per following format.

sub = list[start:end]

The main list will be sliced from the index start to end-1. The following code explains this.

list1 = ["red", "green", "yellow", "blue", "orange"]
list2 = list1[2:4]
print(list2)

The above code will print ['yellow', 'blue'].

We can also specify a step value while slicing in the following format.

sub = list[start:end:step]

Here is an example.

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list2 = list1[2:8:2]
print(list2)

The above code will print [3, 5, 7].

The len() function:

We can find the number of items in a list using the len() function.

colorList = ["red","green","yellow","blue","orange"]
print(len(colorList))

The append() function:

New items can be added to the end of the list using the append() function.

colorList = ["red","green","yellow","blue","orange"]
colorList.append("white")
print(colorList)

The insert() function:

To add new items at any specified position we can use the insert() function. The following code will insert white at the third position.

colorList = ["red","green","yellow","blue","orange"]
colorList.insert(2, "white")
print(colorList)

The remove() function:

To remove a specific item we can use the remove() function. The code below removes green from the list.

colorList = ["red","green","yellow","blue","orange"]
colorList.remove("green")
print(colorList)

The pop()function:

The pop() function removes an item from the specified index or from the end of the list if no index is specified.

The following code removes yellow from the list.

colorList = ["red","green","yellow","blue","orange"]
colorList.pop(2)
print(colorList)

The following code removes orange from the list.

colorList = ["red","green","yellow","blue","orange"]
colorList.pop()
print(colorList)

The del keyword:

The del keyword can also be used to remove an item at a specified index. The following code will remove yellow from the list.

colorList = ["red","green","yellow","blue","orange"]
del colorList[2]
print(colorList)

Note: The del keyword can also be used to delete a list completely.

colorList = ["red","green","yellow","blue","orange"]
del colorList

Traversing through the items in a list using loops:

This will print all the items in the list, one by one.

colorList = ["red","green","yellow","blue","orange"]
for x in colorList:
    print(x)

.

No comments: