Make a List of Data Structures Python
- 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
# Create List empty
color = []
# Make a list of the contents of one item
hobby = [ "coding"]
color= ["red", "blue", "yellow", "green"]
Type any data that can be filled into this List?
cupboard = [ "clothes", 25, True, 34.12]
- "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
color= ["red", "blue", "yellow", "green"]
- red index to 0
- blue index to 1
- yellow index no.2
- green index to 3
# 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]
"soto"
Creating a List Program
- 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
# 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
How to Change Values List
# List first
fruits = [ "oranges", "apples", "mango", "durian"]
# change the index value to 3
fruits [3] = "brown"
["oranges", "apples", "mango", "sapodilla"]
How to Add Item 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
#list fruits
fruits= ["orange", "apple", "mango", "durian"]
# Add mangosteen
fruits.append ( "mangosteen")
["orange", "apple", "mango", "durian", "mangosteen"]
#list fruits
fruits= ["orange", "apple", "mango", "durian"]
fruits.prepend("coconut")
["coconut","orange", "apple", "mango", "durian"]
#list fruits
fruits= ["orange", "apple", "mango", "durian"]
fruits.insert(2, "coconut")
["orange", "apple","coconut", "mango", "durian"]
Creating a Python program with List
# 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)
How to Delete Items in List
# 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
# letter list
letters = ["a", "b", "c", "d"]
# then we delete c
letters.remove ("c")
print letters
cutting list
# 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 ]
[ 'green', 'yellow', 'blue', 'pink']
Use Operations List
- Merger (+)
- Multiplication (*)
# 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 Beginning, No Cry', 'Dear God', 'Break Out', 'Now Loading!!!']
# List of song
playlist_favorite = [
"The Beginning",
"Now Loading!!!"
]
# repeat 5 times
repeat = 5
now_playing = playlist_favorite * repeat
print now_playing
['The Beginning', 'Now Loading!!!', 'The Beginning', 'Now Loading!!!', 'The Beginning', 'Now Loading!!!', 'The Beginning', 'Now Loading!!!', 'The Beginning', 'Now Loading!!!']
List Multi Dimensions
# 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]
"Ice Milk"
# 2-dimensional drink listlist_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
Coffee
Milk
Sweet Tea
Apple Juice
Melon Juice
Orange Juice
Ice Milk
Ice Mix
Ice Teler