Hi,
I’m about to log-off but since it’s been a while and you haven’t been replied to I’ll copy what I wrote in another post. The topic you may want to review is for-loops and lists (at least). But let me know if your question/problem is more specific than that and I can chip in!
- a for-loop is a very common and powerful tool in programming. Don’t let its ubiquity fool you, it is very handy and often has surprising ways of being useful.
- a for-loop can be translated into plain English with a sentence like: for each
item
in your list
, do this action
.
In code that would look like this:
for item in list:
action
Note, that the only 2 hard requisites are that the list be an iterable object (which basically means you can cycle through it. I think of it like numbered key lockers), and that the actions have to be valid.
What is flexible is the naming of the item — it’s just a variable place holder. Often you will see the letter i pop up as a popular variable:
for i in list:
action
So now for a real example:
list_1 = ["pita", "bread"]
list_2 = ["is", "a", "viable", "food"]
for item in list_2:
list_1.append(item)
print(list_1)
#output
#["pita", "bread", "is", "a", "viable", "food"]
In plain English, this code’s commands are to:
For every item in the second list, add that item to the first list. (By default it will go in order, later there are ways to make it go in reverse and all sorts of zany stuff, but basics are important first).