All programming languages provide a way to declare multiple variables through a single statement. Consider a case where you are required to declare 10 variables. You can declare these 10 variables using a unique name. e.g v1,v2,v3…v10.

But what if you are required to declare 1000 variables? Or even 10000 variables? To solve this issue we have a mechanism through which we can declare multiple variables through a single statement. In C and JAVA, this is done using Arrays.

An introduction to python List

In python, the above is done using the concept of a List. A List is a dynamic data-structure, which means that the size of List is not fixed and can be dynamically increased or decreases as data is inserted into it or delete from it.

Declare a List in Python

We can declare a list in Python using following simple statement.

data = []

Where data is the name of variable and [] declares it as an empty list.

Assigned values to List in Python

We can also initialize a list with some values, such as:

data = [1,4, "US",12.5]

This List initialized with 4 initial values (1.2, “US” and 12.5). One thing is worth mentioning is that a List can contain any type of data (integer, float, string etc ) as Python is dynamically typed programming language.

Access elements from a List in Python

Now how to access an element of a List? If we just use the name of List, it means that all elements are selected. e.g following instruction will print the whole list.

print(data)

If we want to access a specific element of a List, you will need to use its index number. That means, each element of a List has a unique index number, through which we can access that element. We can graphically represent the initialized list (data) as follows.

Python List Index

Values written inside the table cells are List initialized values. And values written under, are index number of each element.

As can be seen, each element of List has two index values. e.g element 1 has two index numbers (0 and -4), similarly, 12.5 have two index values (3 and -1 ). So each element of List has two unique index numbers. We can access any an element of List using any Index number. The following code will access the same element of the List:

print(data[1])
print(data[-3])

The first element of List starts with index 0, while the last element starts with index -1. The index is increased by 1 from the First element to Last and decreased by 1 from the Last element to the First Element.
An element of List can be accessed one by one using a ‘for’ loop. For Example:

for element in data:
print(element, " ")

The code above will print the following:

1 4 US 12.5

Add an element to Python List

Also, an element can be added to the end of List by using the append(value) function. As an example, the following statement adds 65 at the end of the List.

data.append(65)

The updated list will be like following.

Python List Updated

Delete an element from Python List

Similarly, the Last element of List can be deleted using the pop() function. For example, the  Following statement deletes the last element, 65, of List.

data.pop()

The updated list will be like following.

Python List Updated

Insert an element to Python List

An element can also be added at a specific index of List using the insert(index,value) method. The two arguments are required by the insert method; the first one is the index number and the second is element value. For example, the following statement inserts an element at index 1.

data.insert(1,345)

The updated list will be like following.

Delete an element from Python List specific index

Similarly, an element from a specific index can also be deleted using the pop(index) function. This function will be required index number as an argument. e.g Following statement deletes index number 3.

data.pop(3)

The updated list will be like following.

There are too many methods associated with List. Description of each method can be found on the official website of Python.

Python List and ‘for’ loop

The following is a simple program that declares an empty list and input five elements from the user and then prints all element of List one by one using a ‘for’ loop:

data=[]
for i in range(5):
data.append[input("Enter A value")]
for j in data:
print(j)

 

Last modified: July 9, 2019

Comments

Write a Reply or Comment

Your email address will not be published.