../_images/book_cover.jpg

This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the content is also available at Berkeley Python Numerical Methods.

The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!

< 2.2 Data Structure - Strings | Contents | 2.4 Data Structure - Tuples >

Data Structure - Lists

We saw strings in the previous section that could hold a sequence of characters. Now, let’s see a more versatile sequential data structure in Python - Lists. The way to define it is to use a pair of brackets [ ], and the elements within it are separated by commas. A list could hold any type of data: numerical, or strings or other types. For example:

list_1 = [1, 2, 3]
list_1
[1, 2, 3]
list_2 = ['Hello', 'World']
list_2
['Hello', 'World']

We could put mixed types in the list as well

list_3 = [1, 2, 3, 'Apple', 'orange']
list_3
[1, 2, 3, 'Apple', 'orange']

We can also nest the lists, for example:

list_4 = [list_1, list_2]
list_4
[[1, 2, 3], ['Hello', 'World']]

The way to retrieve the element in the list is very similar to the strings, see the following figure for the index

List_index

TRY IT! Get the 3rd element in list_3

list_3[2]
3

TRY IT! Get the first 3 elements in list_3

list_3[:3]
[1, 2, 3]

TRY IT! Get the last element in list_3

list_3[-1]
'orange'

TRY IT! Get the first list from list_4.

list_4[0]
[1, 2, 3]

Similarly, we could get the length of the list by using the len function.

len(list_3)
5

We can also concatenate two lists by simply using a + sign.

TRY IT! Add list_1 and list_2 to one list.

list_1 + list_2
[1, 2, 3, 'Hello', 'World']

New items could be added to an existing list by using the append method from the list.

list_1.append(4)
list_1
[1, 2, 3, 4]

Note! The append function operate on the list itself as shown in the above example, 4 is added to the list. But in the list_1 + list_2 example, list_1 and list_2 won’t change. You can check list_2 to verify this.

We could also insert or remove element from the list by using the methods insert and remove, but they are also operating on the list directly.

list_1.insert(2,'center')
list_1
[1, 2, 'center', 3, 4]

Note! Using the remove method will only remove the first occurrence of the item (read the documentation of the method). There is another way to delete an item by using its index - function del.

del list_1[2]
list_1
[1, 2, 3, 4]

We could also define an empty list and add in new element later using append method. It is used a lot in Python when you have to loop through a sequence of items, we will learn more later in the iteration chapter.

TRY IT! Define an empty list and add values 5 and 6 to the list.

list_5 = []
list_5.append(5)
list_5
[5]
list_5.append(6)
list_5
[5, 6]

We could also quickly check if an element is in the list using the operator in.

TRY IT! Check if number 5 is in the list_5.

5 in list_5
True

Using the list function, we could turn other sequence items into a list.

TRY IT! Turn the string ‘Hello World’ into a list of characters.

list('Hello World')
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']

The lists will be used a lot in Python when we are working with data, they have many different use cases that you will see in the later sections.

< 2.2 Data Structure - Strings | Contents | 2.4 Data Structure - Tuples >