How to store multiple data in one variable?
The answer is using a List.
A list is a data structure in python capable of storing more than one piece of data, such as an array.
On this occasion, we will discuss how to use lists in Python from the simplest to the least complex.
What are the points to be studied?
How to make a List and fill it
How to extract values from a List
How to add and remove List contents
Operations on lists
Multi-dimensional List
Happy learning…
How to create a List in Python
We can make a List like a regular variable, but the value of the variable is filled with square brackets [].
Examples:
If the list has more than one content, then we can separate them with commas.
Examples:
What types of data can be included in a List?
The List can be filled with any data type, string, integer, float, double, boolean, object, and so on.
We can also mix the filling.
Examples:
There are four types of data in a list store:
"
book
"
is a string data type;21
is an integer data type;True
is a boolean data type;34.12
How to retrieve values from a List
Now that we know how to create and store data in a List, let's try to retrieve the data.
Just like arrays, lists also have Index numbers to access the data or its contents.
Colors =
“Red” | “Green” | “Yellow” |
0 1 3
List Index numbers always start at zero (0).
This index number is what we need to retrieve the contents (items) of the list.
Examples:
# We have a list of fruits
fruits = ["apple", "grapes", "mango", "orange"]
# For example we will take a mango, then the index is 2
print fruits[2]
Will produce the output:
"mango"
Hooray! got a mango. 😄
Exercise 1: Create a Program with a List
To establish understanding, please try the following exercise.
Make a list to keep your contacts
Fill in the list of 5
Show contents of Index list number 3
Show all friends with looping
Show list length
Let's try the code below …
# Create a list to store the names of friends
my_friends = ["Anggun", "Diana", "Melly", "Jhon", "Adam"]
# Display the contents of the my_friends list with index number 3
print("The content of the 3rd index my_friends is: {}".format(my_friends[3]))
# Show all friends
print("All friends are there {} person".format(len(my_friends)))
for friend in my_friends:
print(friend)
Output results:
Changing The List Value
Lists are mutable, meaning they can be changed.
Examples:
# First list look like below
fruits = ["apple", "grapes", "mango", "orange"]
# Change the value of index to 2
fruits[2] = "coconut"
It "mango" will be replaced with "coconut".
["apple", "grapes", "coconut", "orange"]
Adding A List Item
There are three methods or functions that can be used to add content or items to a List:
append(item) add items from the back.
insert(index, item) adding items from a specific index
Examples:
# First the list are
fruits = ["apple", "grapes", "mango", "orange"]
# Adding mangosteen
fruits.append("mangosteen")
print(fruits)
The result "mangosteen" will be added after the last item. Look result at below:
The second method uses insert().
The method insert() will add items to a specific index of the list.
Examples:
# First the list is
fruits = ["apple", "grapes", "mango", "orange"]
# Adding mangosteen
fruits.insert(1,"mangosteen")
print(fruits)
The result "mangosteen" will be added after the last item. Look result at below:
Delete an Item in the List
To remove one of the contents of the List, we can use the command del.
The command del deletes a variable from memory.
Examples:
# Create List
todo_list = [
"Learn Python",
"Learn Django",
"Learn MongoDB",
"Learn Magic",
"Learn Flask"
]
# Suppose we want to remove "Learn Magic"
# which is on the 3rd index
del todo_list[3]
print(todo_list)
Look the output at below:
As a result, "Learn Magic" will be deleted:
Examples:
# First we have a list
a = ["a", "b", "c", "d"]
# then delete b
a.remove("b")
print(a)
Results :
Actually, there is still a lot I want to discuss about the list, but since this article is quite long...
I end it here.
Next, please experiment yourself with the list.
No comments: