Make a List of Data Structures Python

Make a List of Data Structures Python

How the hell do we to be able to store more data in just one variabel?

The answer is by using a List.

List is a data structure in python program that can store more than one data, as well as an array.

On this matter, we will discuss how to use the list in a Python program from the simplest example to the rather complex.

What things will we learn?
  • How to Make a List and Fill
  • How to Grep value of List
  • How Adding and Removing contents List
  • Learn Operations on List
  • and multi-dimensional List

How to Make a List in Python

List we can use as we make regular variable, but the value of the variable is filled with square brackets [] .

Example code:

# Create List empty 
color = []

# Make a list of the contents of one item 
hobby = [ "coding"]

If the list has more than one value or contents, then we can separate it with commas.

Example code:

color= ["red", "blue", "yellow", "green"]

Type any data that can be filled into this List?

list can be filled with any type of data whether it is a string, integer, float, double, boolean, object, and so on.

We can also mix in the contents.

Example code:

cupboard = [ "clothes", 25, True, 34.12]

There are four types of data in the list of the closet :
  • "Clothes" is a string data type;
  • 25 is an integer data type;
  • True is a boolean data type;
  • and 34.12 is a data type float.

How To Take Values ​​from a List

After the above material we know how to create and store a value in the List, let's try to take away its value.

As well as an array list, the list also has an index number to access the data or its contents.

color= ["red", "blue", "yellow", "green"]

  • red index to 0
  • blue index to 1
  • yellow index no.2
  • green index to 3

List the index number always starts from zero (0).

This index number that we need to take the contents (values) from the list.

Example code:
# We have a list of fruit names
food = ["rice", "meatballs", "soto", "noodles"]
# For example we want to take soto
# Then the index is 2
food print [2]
Results of the program will produce the output:


Creating a List Program

To solidify the understanding of the list in python, please try the following exercise.
  • Create a list to store the names of friends
  • Contents list of 5
  • Show the contents of the list index number 4
  • View all friends with looping
  • Show long list
Let us answer his training with the following code:
# Create a list to accommodate the names of friends
my_friends = ["Fajar", "Eka", "Andi", "Wahyu", "Nanda"]
# Display the contents of my_friends list with index number 4
print "Fill in my_friends 4th index: {}". format (my_friends [4])
# Show all friend lists
print "All friends: there are {} people". format (len (my_friends))
for friend in my_friends:
print friend

In the above code, we use the function len () to retrieve the length of the list.

Results output:

Make a List of Data Structures Python

How to Change Values ​​List

Lists are mutable, meaning that the contents can change.

Example code:

# List first 
fruits = [ "oranges", "apples", "mango", "durian"] 
# change the index value to 3 
fruits [3] = "brown"

Then the "durian" are replaced by "sapodilla".

["oranges", "apples", "mango", "sapodilla"]

How to Add Item List

There are three methods (method) or function on a python program that can be used to add content or items to the List:
  • prepend (items) add items from the front;
  • append (item) adds an item from behind.
  • insert (index, item) adding an item of a particular index
Example code:

#list fruits
fruits= ["orange", "apple", "mango", "durian"]
# Add mangosteen
fruits.append ( "mangosteen")

The result is "mangosteen" would be added after the last item.

["orange", "apple", "mango", "durian", "mangosteen"]

The second method uses prepend ().

Methods prepend () will add an item from the front or the beginning of the list.


#list fruits
fruits=  ["orange", "apple", "mango", "durian"]

Then the "coconut" would be added at the beginning of the list.

["coconut","orange", "apple", "mango", "durian"]

Besides prepend () and append () we can use the method insert () to add the item to the index that we want.

Example code:

#list fruits
fruits= ["orange", "apple", "mango", "durian"]
fruits.insert(2, "coconut")

And the list would be like:

["orange", "apple","coconut", "mango", "durian"]

Creating a Python program with List

Now let's try to create a program by utilizing the method prepend () and append ().

Please direct typed and tested under the program coding:
# Create empty lists to accommodate hobbies
hobby = []
stop = False
i = 0
# Fill in a hobby
while (not stop):
new_hobby = raw_input ("Input hobby that is {}:". format (i))
hobby.append (new_hobby)
# Increment i
i += 1
ask = raw_input ("Want to fill again? (y/n):")
if (ask == "n"):
stop = True
# Print All Hobbies
print "=" * 10
print "You have {} hobby". format (len(hobby))
for hb in hobby:
print "- {}". format (hb)

Try execution and fed a hobby you want value then the result will be like this:

Make a List of Data Structures Python

How to Delete Items in List

To remove one of the values ​​of the List, we can use the command del.

Del command will erase the memory of a value of the variable.

Example code:
# Make a List
todo_list = [
"Learning Python",
"Learning PHP",
"Learning HTML",
"Learning CSS",
"Learning JAVA"
# Suppose we want to delete "Learning CSS"
# which is in the 3rd index
del todo_list [3]
print todo_list

As a result, "Learning CSS" will be deleted:

Make a List of Data Structures Python

In addition to using the command del, we also can use the method remove () with a parameter value to be deleted.

Example code:
# letter list
letters = ["a", "b", "c", "d"]
# then we delete c
letters.remove ("c")
print letters

The result:

Make a List of Data Structures Python

cutting list

Like the string data type, the list can also be cut into pieces.

Example code:

# We have a list of colors 
colors = [ "red", "green", "yellow", "blue", "pink", "purple"] 
# We cut from the index to the 1st until the 5th 
print color [1: 5 ]

The result is an index of 0 [red] and 6 [purple] will not be displayed:

[ 'green', 'yellow', 'blue', 'pink']

Use Operations List

There are some operations that can be performed on the List, including:

  • Merger (+)
  • Multiplication (*)
Example code:

# List of songs 
list_song= [ 
    "The Beginning", 
    "Dear God" 

# list of favorite songs 
playlist_favorite = [ 
    "Break Out", 
    "Now Loading !!!" 

# Let us combine both 
all_song = all_song + playlist_favorite 
print all_song 

The result:

['The Beginning, No Cry', 'Dear God', 'Break Out', 'Now Loading!!!']

As for the multiplication operation can only be performed with a value that is a number.

Example code:

# List of song
playlist_favorite = [
    "The Beginning",
    "Now Loading!!!"
# repeat 5 times
repeat = 5
now_playing = playlist_favorite * repeat 
print now_playing

The result:

['The Beginning', 'Now Loading!!!', 'The Beginning', 'Now Loading!!!', 'The Beginning', 'Now Loading!!!', 'The Beginning', 'Now Loading!!!', 'The Beginning', 'Now Loading!!!']

List Multi Dimensions

In the above material, we just make the program one-dimensional list.

List can also have more than one dimension, or so-called multi-dimensional.

Multi-dimensional list is usually used to store complex data structures such as tables, matrices, graphs, tree, etc.

Example code:

# List of drinks with a 2 dimensional 
list_drinks = [ 
    [ "coffee", "Milk", "tea"], 
    [ "Apple Juice", "Juice Melon", "Orange Juice"], 
    [ "Ice Milk", "Ice Mixed" "Ice Teler"] 

# How to access the list of multidimensional 
# suppose we want to take the "Ice Milk" 
print list_drinks [2] [0]

Dyad 2 in the above code, attribute index will we access list. Then after to list its new take its contents.

Results output:

"Ice Milk"

What if we want to display all contents in a multi-dimensional list above?

Here's how, Try using nested loops.
# 2-dimensional drink list
list_drinks = [

    ["Coffee", "Milk", "Sweet Tea"],
    ["Apple Juice", "Melon Juice", "Orange Juice"],
    ["Milk Ice", "Ice Mix", "Ice Teler"]

for menu in list_drinks :
    for drinks in menu:
        print drinks 
The result:

Sweet Tea 
Apple Juice 
Melon Juice 
Orange Juice 
Ice Milk 
Ice Mix
Ice Teler

Spread the love

Fajar (Ucup)

The owner and author of and also someone who likes to share interesting information with others.

Leave a Reply

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