DEV Community

Alfa Riza
Alfa Riza

Posted on

Complete List in Python

Assalamualaikum warahmatullahi wabarakatuh. Hello everyone, on this occasion we will discuss about lists in python.
List is a data type in python for storing variable items. In the list we can store several types of variables at once. This list is sorted from index 0 and can be changed, added, deleted.image

Make a List

How to make a list, by creating open and closed brackets and to separate one variable from another with a comma.

angka = [1,2,3,4]
list1 = ["satu", 2, True]
Enter fullscreen mode Exit fullscreen mode

Reading List

We can read the list by accessing its index. For example, we have a list like this:

list1 = ["Dosen", "Mahasiswa", "Satpam", "Driver"]
Enter fullscreen mode Exit fullscreen mode

to be able to print Students, we can access list1 with index 1.

print(list1[1])
Enter fullscreen mode Exit fullscreen mode

If we want to print all the data in the list, we can use loops.

for l in list1:
   print(l)
Enter fullscreen mode Exit fullscreen mode

Changing List

We can change the value in the list by:

list1 = ["Python", "PHP", "Ruby"]
list1[1] = "Golang"
Enter fullscreen mode Exit fullscreen mode

In the code above, we change PHP to Golang

Add List

We can also add to the list we have with the append() and insert() functions, both of which have differences.
In the append() function, the data we enter will automatically be added to the last index.

list1 = ["Python", "PHP", "Ruby"]
list1.append("Golang")
Enter fullscreen mode Exit fullscreen mode

The code above adds “Golang” to list1, where “Golang” will be added to the end. So that

list1 = ["Python", "PHP", "Ruby", "Golang"]
Enter fullscreen mode Exit fullscreen mode

Whereas in the insert() function, we need 1 more parameter, where this parameter is a specific index that we want to put. For example

list1 = ["Python", "PHP", "Ruby"]
list1.insert(1, "Golang")
Enter fullscreen mode Exit fullscreen mode

The code above, adds "Golang" to list1 with index to 1. So that list1 becomes

list1 = ["Python", "Golang", "PHP", "Ruby"]
Enter fullscreen mode Exit fullscreen mode

Delete List

To delete a list, we can delete one or all data. We will discuss how to delete a single data first, to remove specific data, we can use the remove() function

list1 = ["Python", "PHP", "Ruby"]
list1.remove("Python")
Enter fullscreen mode Exit fullscreen mode

The above code will delete list1 which has "Python" data, so list1

list1 = ["PHP", "Ruby"]
Enter fullscreen mode Exit fullscreen mode

In addition to specific data, we can also delete specific indexes using the pop() function and the del keyword. the pop() function has an index parameter which we will remove.

list1 = ["Python", "PHP", "Ruby"]
list1.pop(1)
Enter fullscreen mode Exit fullscreen mode

The above code removes list1 with index 1, so list1.

list1 = ["Python", "Ruby"]
Enter fullscreen mode Exit fullscreen mode

If the pop() function does not have an index parameter, then the data to be deleted is the last data.

list1 = ["Python", "PHP", "Ruby"]
list1.pop()
list1 = ["Python", "PHP"]
Enter fullscreen mode Exit fullscreen mode

As for the del keyword how to use it as follows:

list1 = ["Python", "PHP", "Ruby"]
del list1[0]
list1 = ["PHP", "Ruby"]
Enter fullscreen mode Exit fullscreen mode

Deleting all data lists we can also use the del keyword and the clear() function. How to use the del keyword, namely:

list1 = ["Python", "PHP", "Ruby"]
del list1
list1 = []
Enter fullscreen mode Exit fullscreen mode

while the use of the clear() function

list1 = ["Python", "PHP", "Ruby"]
list1.clear()
list1 = []
Enter fullscreen mode Exit fullscreen mode

Sort List

If we have a list whose data is not sorted, we can sort it with the sort() function. This function can be used as a string or number data type. The sort function will automatically sort from small to large or ascending.

list2 = [1,9,4,28,17]
list2.sort()
list2 = [1,4,9,17,28]
Enter fullscreen mode Exit fullscreen mode

If we want to sort from largest to smallest or descending, we can add the reverse = True parameter.

list2 = [1,9,4,28,17]
list2.sort(reverse = True)
list2 = [28,17,9,4,1]
Enter fullscreen mode Exit fullscreen mode

Copy List

If we want to copy list then we can't use code

list2 = list1
Enter fullscreen mode Exit fullscreen mode

Because list2 will reference list1, if list1 changes, list2 will automatically change as well. To overcome this, we can use the copy() and list() functions.

list1 = ["Python", "PHP", "Ruby"]
list2 = list1.copy()
list2 = ["Python", "PHP", "Ruby"]
Enter fullscreen mode Exit fullscreen mode

Or we can add list()

list1 = ["Python", "PHP", "Ruby"]
list2 = list(list1)
list2 = ["Python", "PHP", "Ruby"]
Enter fullscreen mode Exit fullscreen mode

Merge List

If we have 2 lists, then we want to combine them. Then the simplest way is to use the + operator.

list1 = ["Python", "PHP", "Ruby"]
list2 = ["Golang", "Vue", "React"]
list3 = list1 + list2
list3 = ["Python", "PHP", "Ruby", "Golang", "Vue", "React"]
Enter fullscreen mode Exit fullscreen mode

Or we can also use the extend() function.

list1 = ["Python", "PHP", "Ruby"]
list2 = ["Golang", "Vue", "React"]
list1.extends(list2)
list1 = ["Python", "PHP", "Ruby", "Golang", "Vue", "React"]
Enter fullscreen mode Exit fullscreen mode

That's enough for us to discuss the list, thank you :)

Top comments (0)