Python list introduction

We use python a list to store multiple items in a single variable. Therefore just like a String, it allows us to slice.

Moreover, we can manipulate a python list at our whims. 

Why? Because the python list is mutable. We can change any value of the python list.

For example, we consider two lists where the power of the numbers ascend and descend.

list_of_powers_in_asc = [2**0, 3**1, 4**2, 5**3]
print(list_of_powers_in_asc)

list_of_powers_in_dsc = [2**3, 3**2, 4**5, 5**0]
print(list_of_powers_in_dsc)

#output:
[1, 3, 16, 125]
[8, 9, 1024, 1]

However, while creating the list, we have noticed that the order of the numbers in the second list was wrong.

Therefore, we need to rectify it.

As the python list is mutable, we can do it in an easy manner.

# rectifying the wrong order
list_of_powers_in_dsc = [2**3, 3**2, 4**5, 5**0]
list_of_powers_in_dsc[2] = 4**1
print(list_of_powers_in_dsc)

#output
[8, 9, 4, 1]

The slicing of the list is the same as python String.

print(list_of_powers_in_dsc[-1])
#output
1

print(list_of_powers_in_dsc[-3:])
#output
[9, 4, 1]

print(list_of_powers_in_dsc[:-1])
#output
[8, 9, 4]

Appending the list is also easy. As a result, we have a completely new list with a new value.

list_of_powers_in_asc = [2**0, 3**1, 4**2, 5**3]
list_of_powers_in_asc.append(6**4)
list_of_powers_in_asc.append(7**5)
print(list_of_powers_in_asc)

#output
[1, 3, 16, 125, 1296, 16807]

As we said earlier, we can slice the list as we did in the case of python string.

list_of_powers_in_asc[1:3] = [] # 1 inclusive, 3 is not, upto 2
print(list_of_powers_in_asc)

#output
[1, 125, 1296, 16807]

Finally we will take a look at how we can nest lists.

We can nest as many lists as we wish.

However, that means we’re adding new dimensions.

As the number of dimensions increases, it becomes hard to track the value.

In such cases, we can use python libraries like NumPy.

Let’s see a simple nested list where inside two square brackets we have two lists.

list_of_powers_in_asc = [2**0, 3**1, 4**2, 5**3]
print(list_of_powers_in_asc)

list_of_powers_in_dsc = [2**3, 3**2, 4**1, 5**0]
print(list_of_powers_in_dsc)

nested_list = [list_of_powers_in_asc, list_of_powers_in_dsc]
print(nested_list)
nested_list[0][2]

#output
[1, 3, 16, 125]
[8, 9, 4, 1]
[[1, 3, 16, 125], [8, 9, 4, 1]]
16

nested_list[1][2]
#output
1024

Since we have used one nested list, the index 0 [0], points to the first list.

As an outcome, the second index number [2], points to the elements of the first list.

We’ll come with more python concepts for beginners. 

So stay tuned.

If you want to get other code snippets as adobe, please the respective GitHub Repository.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

TensorFlow, Machine Learning, AI and Data Science

Flutter, Dart and Algorithm

Twitter


Posted

in

by

Comments

One response to “Python list introduction”

  1. […] In previous sections, we have examined different features of python. […]

Leave a Reply