List in Python programming language | applied electronics engineering


List in Python programming language

By Applied Electronics - Friday, May 13, 2016 No Comments
List is build-in sequence object in Python programming language. List is also a sequential object, meaning that the elements are assigned positional values. The items in the list can be any objects- numbers, strings and even another list or dictionaries. The items in the list can be removed or modified and therefore list is mutable object.

In previous python tutorials, we showed you how to work with numbers and strings. Numbers and strings are fundamental objects in Python programming language but they are not the only one. List and dictionaries are among others the core objects in python programming language. It is list and dictionaries that makes python a powerful programming language. Here we look into list.

In this python list tutorial, we show how to create list, how to access list items, how to get multiple list items using indexing and slicing operation on list, how to use the list method and how to get help for list in python.

Creating a list

Creating a list is simple. Use a square bracket and put the objects inside the bracket and seperate them using commas. If it is a object item is string then use either single or double quotes.

>>> L = ['John', 45, ['firstname','surname']]

is an example of a list

Accessing List elements/items

You can perform indexing operation to get the items of the list.

>>> L[0]
>>> L[1]
>>> L[2]
['firstname', 'surname']

The items are sequentially numbered starting from 0.

Extracting data from List

When we use index range of the form [a:b] or [a:b:c] where a, b and c are index numbers to extract part of list data then the operation is called slicing. Such operations are useful to extract for example, part of text from a larger text.

Slicing with List

We show you how slicing works with a number list. But the same rule applies with strings or any other objects in Python.

Consider you have the number list as follows.

>>> numbers = [0,1,2,3,4,5,6,7,8,9]
>>> numbers
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> numbers[0:1]
>>> numbers[0:2]
[0, 1]
>>> numbers[0:3]
[0, 1, 2]
>>> numbers[5:]
[5, 6, 7, 8, 9]
>>> numbers[5:10]
[5, 6, 7, 8, 9]

numbers[:] means all elements:

>>> numbers[:]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Indexing and slicing operation should remind you about arrays which you may know from other programming languages. We can say, List are like arrays found in other programming languages.

In fact we create matrix using list. Let us create 3 by 3 matrix:

>>> M = [[1,2,3],[4,5,6],[7,8,9]]
>>> M
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

You printed representation might not look like a matrix but it is. We can access the matrix elements using the following:

>>> M[2][2]

That is we extract 3rd row, 3rd column matrix by performing index operation using two square brackets since matrix is two dimensional objects.

To get the 2nd row:

>>> M[1]
[4, 5, 6]

To get the 1st column:

 >>> [row[0] for row in M]
[1, 4, 7]

 Skipping list elements

Next consider that you wanted to skip elements after each 2 elements then you could do the following:

>>> numbers[0:len(numbers)-1:2]
[0, 2, 4, 6, 8]

len(x) gives the length of the object, in this case the list called numbers. The last element is the stepping distance which is by default 1 but can be changed as in was done in the above case.

If you just want every 3rd element then just use the following:

>>> numbers[::3]
[0, 3, 6, 9]

List Range

In the above example, we use a list containing all numbers from 0 to 9. We can generate such numbers using range method. Here is how:

 >>> numbers = list(range(0,10))
>>> numbers
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Guess what happens if we added a 3rd argument for range:

>>> numbers = list(range(0,20,2))
>>> numbers
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Adding a 3rd argument, as you may have expected, generates numbers from start to end with steps defined by the third argument. Notice we have used list( ) method.

list method

List method or function finds application where we want to refer to list. One example was just shown above where we used list method to list the number in certain range. Another example where list method is often used in python programming is to list the string content. That is given a string, we can list the characters that makes up the list.

>>> s = "what is bacteria"
>>> list(s)
['w', 'h', 'a', 't', ' ', 'i', 's', ' ', 'b', 'a', 'c', 't', 'e', 'r', 'i', 'a']

help and dir of List

The subject of List is vast, so if you want to know more about List you can use the following commands:


No Comment to " List in Python programming language "