Lists in Python

Lists in Python

List is another type of Python variable that allows you to store different and multiple values in order to do any kind of programming process you want to do. Each item in the list has an index or position and these items are separated by commas.
You can change the items of the list because it is a mutable type and access them by their index. You can use different built-in Python functions and methods to deal with the data that is inside the list.

Syntax

#____ProgrammerCodes.com____
listName = [value1,value2,value3,value4]
#____ProgrammerCodes.com____

Example

#____ProgrammerCodes.com____
studentsAge = [19,20,21,19,21]
print(studentsAge)
#____ProgrammerCodes.com____

Result

[19, 20, 21, 19, 21]
list in python

How to access a list in Python?

To access any value in the list, you have to know its index or position in the list and write it inside []. You can access multiple items by writing their index in this way: for example, I want to access 2 items that are in the position 2 and 3 so I will access them by doing listName[2:4]

How to access a list in Python

Syntax

#____ProgrammerCodes.com____
listName[index]
#____ProgrammerCodes.com____

Example 1 (Access only one item of the list)

#____ProgrammerCodes.com____
studentsAge = [19,20,21,19,21]
print(studentsAge[2])
#____ProgrammerCodes.com____

Result

21
Access only one item of the list

Example 2 (Access two items of the list)

#____ProgrammerCodes.com____
studentsAge = [19,20,21,19,21]
print(studentsAge[2:4])
#____ProgrammerCodes.com____

Result

[21, 19]
Access two items of the list

In order to add a value inside the Python list, you have to write the list name then write the position where you want your value to be. After doing that, you will have to write the value.

You can add two or more new items at the same time including their positions using the slicing way [position1:position2].

If you want to add a new value without changing an old item, you will have to use the append function. We will explain it later.

Syntax

#____ProgrammerCodes.com____
studentsAge[index] = value
#____ProgrammerCodes.com____

Example 1 (Add only one item)

#____ProgrammerCodes.com____
studentsAge = [19,20,21,19,21]
studentsAge[2] = 22
print(studentsAge)
#____ProgrammerCodes.com____

Result

[19, 20, 22, 19, 21]
Add only one item

Example 2 (Add two items)

#____ProgrammerCodes.com____
studentsAge = [19,20,21,19,21]
studentsAge[0:2] = 22 , 18
print(studentsAge)
#____ProgrammerCodes.com____

Result

[22, 18, 21, 19, 21]
Add two items

How to delete an item in a list in Python?

It is easy to delete an item that is inside a list in Python by writing the name of the list and adding the index or position of the value. You can delete multiple items with the slicing way.

How to delete an item in a list

Syntax

#____ProgrammerCodes.com____
del listName[index]
#____ProgrammerCodes.com____

Example 1 (Delete one item of the list)

#____ProgrammerCodes.com____
studentsAge = [19,20,21,19,21]
print("The old list: ", studentsAge)
del studentsAge[0]
print("The new list: ", studentsAge)
#____ProgrammerCodes.com____

Result

The old list: [19, 20, 21, 19, 21]
The new list: [20, 21, 19, 21]
Delete one item of the list

Example 2 (Delete multiple items of the list)

#____ProgrammerCodes.com____
studentsAge = [19,20,21,19,21]
print("The old list: ", studentsAge)
del studentsAge[0:4]
print("The new list: ", studentsAge)
#____ProgrammerCodes.com____

Result

The old list: [19, 20, 21, 19, 21]
The new list: [21]
Delete multiple items of the list

Example 3 (Delete the entire list)

#____ProgrammerCodes.com____
studentsAge = [19,20,21,19,21]
print("The old list: ", studentsAge)
del studentsAge
print("The new list: ", studentsAge)
#____ProgrammerCodes.com____

Result

The old list: [19, 20, 21, 19, 21]
Traceback (most recent call last):
File "C:/Users/Admin/Desktop/test.py", line 4, in
print("The new list: ", studentsAge)
NameError: name 'studentsAge' is not defined
Delete the entire list

The error is because we have deleted the list. It doesn’t exist anymore.

How to find length of a list in Python?

The len() function helps us to get the total length of the list. You will get how many items your list has. The simple way to explain this function is that this len() will give you the number of index or positions of the list.

Syntax

#____ProgrammerCodes.com____
len(listName)
#____ProgrammerCodes.com____

Example

#____ProgrammerCodes.com____
studentsAge = [19,20,21,19,21]
print("The total number of students is: ", len(studentsAge))
#____ProgrammerCodes.com____

Result

The total number of students is: 5
 How to find length of a list in Python

How to append to a list in Python?

Use the function append() if you want to add more items to the list.

Syntax

#____ProgrammerCodes.com____
listName.append(value)
#____ProgrammerCodes.com____

Example

#____ProgrammerCodes.com____
studentsAge = [19,20,21,19,21]
print("The old list: ", studentsAge)
studentsAge.append(18)
print("The new list: ", studentsAge)
#____ProgrammerCodes.com____

Result

The old list: [19, 20, 21, 19, 21]
The new list: [19, 20, 21, 19, 21, 18]
How to append to a list in Python

Tutorial’s Question

What is the correct syntax to delete only one item in the list?

  • del listName[value]
  • del listName()
  • del listName[index]

Simple Examples

If you have learned the basics of Python and you want to see some simple examples about these concepts such as numbers, for loop, function, and so on, I suggest you visit these articles below:

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *