Basic List Operations in Python

Basic List Operations in Python

Understanding how to go around creating a list and doing some logical operations with a list in python is an intriguing part of python one needs to understand. in this article, you will learn the list concept and how to create a list, and several operations you can perform with a List in Python, without wasting much time let's get started.

What's a List?

A list is a collection of more than one item in a variable enclosed in a square bracket. "[]", they are ordered and changeable, a list also allows us to create a duplicate of its members. Generally, a list is used to store multiple items into a variable, and we can store strings, integers, Boolean, or any datatype into a list.

Example

A string list.

Python

List_of_cars= ["Volvo", "Benz", "BMW", "Hyundai", "Honda"]

An integer list.

int_list= [12, 35, 45, 78, 67]

A Boolean list.

Bool_list= [True, False]

We can also create a list with different data types altogether.

Different_types= [12, "Hyundai", True, 67, "BMW"]

But throughout this course, we will use the string_list.

Accessing an item in a List

Items in a list can be accessed through their index number,

Example: To access the third item in the above list.

List_of_cars= ["Volvo", "Benz", "BMW", "Hyundai", "Honda"]
print(List_of_cars[3])

Output:

Hyundai

we referred to the third index number.

Note: index numbers start it count from 0.

Add an item to the list

We can add an item to a list by using the append() syntax. For instance, we want to add a new model of car to the list we already have.

List_of_cars.append("Maybach")

Output:

["Volvo", "Benz", "BMW", "Hyundai", "Honda", "Maybach"]

There is another way with which we can add an item to a list, which is using the insert(). This method inserts a single item into a specific position in the list.

Example: For instance, we want to add Maybach to the list as the third element of the list, we state the position first before adding the item to the code.

List_of_cars.insert(3, "Maybach")
print('Updated list': List_of_cars)

Output:

["Volvo", "Benz", "BMW", "Maybach", "Hyundai", "Honda"]

Removing an item from a List

Items in a list can be removed with four different keywords, we have the pop() keyword, clear() keyword, del() keyword, and lastly the remove() keyword. These keywords

a. The pop() keyword is used to remove an item by its index number i.e., from a specific position.

Example:

List_of_cars= ["Volvo", "Benz", "BMW", "Hyundai", "Honda"]
print(List_of_cars.pop(2))

Output:

["Volvo", "Benz", "Hyundai", "Honda"]

b. The clear() keyword is used to remove every element we have available in a list. Example:

List_of_cars= ["Volvo", "Benz", "BMW", "Hyundai", "Honda"]

List_of_cars.clear()

Output:

[ ]

Note the empty square brackets notifies us that there is a list present, but it has been emptied.

c. The del keyword removes an item from a list by its slice or index number.

Example:

List_of_cars= ["Volvo", "Benz", "BMW", "Hyundai", "Honda"]
print(List_of_cars)

del List_of_cars[3]
print(List_of_cars)

Output:

["Volvo", "Benz", "BMW", "Hyundai", "Honda"]
 ["Volvo", "Benz", "BMW", "Honda"]

Lastly, under this section we have,

d. the remove() keyword, this keyword help to remove a given item, The element to be removed is always provided in the codebase.

Example:

["Volvo", "Benz", "BMW", "Hyundai", "Honda"]
remove("Volvo")
print(List_of_cars)

Output:

 ["Benz", "BMW", "Hyundai", "Honda"]

## Changing an item in a List

We can easily change an item in a list by simply locating the index number and referring a new element to it

Example:

List_of_cars= ["Volvo", "Benz", "BMW", "Maybach", "Hyundai", "Honda"]

List_of_cars[4] = "Maserati"

Output:

 ["Volvo", "Benz", "BMW", "Maybach", "Maserati", "Honda"]

How to Loop Through a List

We can loop through a list by using the for keyword.

Example:

List_of_cars= ["Volvo", "Benz", "BMW", "Maybach", "Hyundai", "Honda"]

for car in List_of_cars:
print(car)

Output:

Volvo
Benz 
BMW 
Maybach
Hyundai
Honda

##Wrapping Up##

Finally, we have been able to explain what a list is, and we have gone through all the basic operations we can perform with a list in Python language, with this knowledge we can perform different operations with a list and also recognize a list in any codebase we come across. Happy coding.