How to Use List in Python

Posted on

Introduction

This is an article where the content is mainly sharing information for using list in python programming language. In this article, there will be several aspects for further description about list. Starting from how to declare or define the list, until how to process it further. Further processing includes how to add, edit and remove data from list.

How to Use List in Python

So, as it is already exist in the description from the introduction part, this article will focus on the usage of list for several aspects. Starting from declaration and definition of list, into another aspect for processing the list including to add, edit and remove data.

Declaring and Defining List

In order to define and declare list, it is very simple. There are two type of definition or declaration for the list. The first one is an initial definition or declaration without any data. Defining and declaring list without any data only need a square bracket as follows :

list_example = []

Furthermore, the second on is definitely, the opposite from the first one. Instead of defining without any data, the second one is defining or declaring with available data. So, in order to define or declare list using several data, the following is an example :

list_example = [1,2,3,4]

Adding Data in List

After successfully defining and declaring variable with a list data type, just move forward to another process. Certainly, if currently there is no data available in the list, just perform a simple process. That simple process is adding a new data or a new element to the list. So, in order to add a new data or new element, the following is the syntax to perform it :

list_example.append(data);
(env) C:\python>python
Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> list_example = [];
>>> list_example.append(1);
>>> print(list_example);
[1]
>>>

Removing Data in List

Another important aspect which is also available in term of list collection data type usage, it is the removal process of the data or the element exist in the list. In order to do that, the following is an example for removing data or element in a list continuing from the above example :

(env) C:\python>python
Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> list_example.remove(1);
>>> print(list_example);
[]
>>>
Test

Replacing Data in List

Besides adding and also removing data in list, there is also another important aspect or function which is available in list collection data type. Actually, it is the ability to replace any data or any element exist in the list. Below is the steps with an example for replacing data in a list continuing from the previous example :

(env) C:\python>python
Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> list_example = [];
>>> list_example.append(1);
>>> list_example.append(2);
>>> list_example.append(3);
>>> print(list_example); 
>>> [1,2,3]
>>> list_example[1] = 'Two';
>>> print(list_example);
[1,'Two',3]

Leave a Reply