Data Structures in Python – Part 1

Tram Ho

The basic data types in Python include integer type (int), real number type (float), string type, and boolean type. With these basic data types, we can write simple applications with Python.

For applications with a large number of variables, or big data, we need data structures that make coding short and efficient. In Python, there are 4 types of data structures: List, Tuple, Set and Dictionary.

Why data structure is needed

Data structures are used in programs to make it easier to locate information and retrieve it. Data structure is how programming languages ​​express basic values, they contain basic data types such as numbers, strings, booleans …, it gives a way to store multiple values ​​in a variable. number.

Data structures are also used to group and organize for other structures.

1. List data structure

1.1 Definition and usage of List

List is the simplest data set in Python, a List is a list of data elements separated by commas and surrounded by square brackets. Lists can contain numbers or strings.

Note: A list can contain many different types of data if you deem necessary, but it is advisable to stop a consistent data type for the elements of the list.

1.2 Retrieve elements in List

Each element in the List will have a certain position corresponding to a number, starting from 0 and increasing gradually. We can access the items in the list with the syntax:

In the above example, to access the name Van, we do the following:

Note: If you have access to an item that is not in the list, an error will appear of the form “index out of range” .

1.3 Multi-dimensional list

Each item in Python’s list can be a list, for example the following list, each element is a list of names and ages of friends.

In Python there is no limit to the number of dimensions of the list as well as the number of elements the list can contain, it just depends on the amount of memory the computer uses to run the application.

Note, for long lists, we should display each element on one line, so the code will be easier to read, for example:

1.4 Some functions related to list

In the process of manipulating the list, we need to: check if the list contains a certain element, count the number of elements or add, delete, edit the element in the list … These requirements are all There are built-in operators or methods in the List object.

1.4.1 Print operator

The print operator allows you to check if an element is in a list. Example: Check if “Dung” is in the members list?

1.4.2 Wool ()

The len () function returns the number of elements in a list.

1.4.3 Some methods on Model List

.append () adds an element to the last position in the List:

.insert (position, item) inserts an element to List at the given position.

The elements behind will be positioned up by 1, pay attention when accessing these elements.

listname [index] = newvalue Changes the value of an element whose index position in the List.

.extend () Combine a list with another List.

.remove () Removes an element from the List.

.pop (index) Removes the element at the specified index position in the List.

del Deletes an element or the entire list

.clear () Cleans the items of the list

Note, .clear () is different from del, .clear () removes all elements in List, and del deletes also the variable List.

.count () Counts the number of times an element appears in the List.

.index () Returns the position of the element in List

.sort () sorts the elements in the List.

The default sort order with string data will sort az, AZ, with numbers 0-9. For descending sort use the parameter reverse = True.

reverse () Reverses the order of the elements in the List.

.copy () Copy the entire List.

2. Tuple data structure

Python supports a data structure similar to List named Tuple, only one difference Tuple is an immutable list, can not change the content. That is, right after the definition of Tuple, you cannot change it.

2.1 Tuple declaration

Defining a Tuple is like defining a List in Python, except that instead of using square brackets, we use parentheses.

Attention:

Python allows the definition of Tuple without using the parentheses. For example:

Even Tuple has only one element:

Note the comma, if there is no comma, this is a definition of a string.

2.2 Working with Tuple

Tuple is a special list that cannot be changed once created, so you can use all the techniques and functions as with List but exclude the ones that change the content.

For example, you can use the print operator, the len () function with Tuple:

All the .append (), .extend (), .clear (), .copy (), .insert (), .pop (), .remove (), .reverse (), .sort () methods don’t usable with Tuple data structures.

Note: It is not possible to change the Tuple but it is possible to create a Tuple from two Tuples, for example:

2.3 Why use Tuple

Tuple has limitations that when created can not be changed, but Tuple has the following advantages:

  • Tuple has faster processing speed than List, because Tuple is stored a specific block of memory and List often has to change storage space. If you define a set of values ​​as constants, Tuple should be chosen.
  • Using Tuple makes the code more secure, so the “write-protect” mode makes the data immutable. Therefore, it is recommended to choose data in the form of constants, data that do not change over time.
  • Tuple is also used as a key in Dictionary because it contains constant values, List is not used as a key for Dictionary.
Share the news now

Source : Viblo