Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Super Quick Python Arrays & Loops Examples

Ghost

Platinum Coder
Hey all,

I am sitting in my car waiting for my girlfriend to be done with work... sharing a car this morning ! So I decided to write up some basic Python loops.

Our first array just holds some users and our simple for loop prints out a sentence saying each user is cool.
Python:
users = ["GHOST","MALCOLM","ROOT"]
for user in users:
    print(user + " is cool.")
print("\n")


Then we have an array of users/threads
We use the username as the key and the thread ID lists are stored in a little sub array for each user. Because of this, we can use the items() function in Python to access both the key (username) and value (thread ID list).
We just check the threadIDlist to see if there's more than 0 results (ex: ROOT has 0 threads in their list).
Python:
data = {"GHOST":[1,3,5],"MALCOLM":[0,2,4,6], "ROOT":[]}
threads = ["Forum Rules", "HELLO", "WELCOME TO CF", "I AM A GHOST", "How to use Code Forum", "Do you know PHP?", "Where do you find stock images?"]
for username, threadIDlist in data.items():
    if len(threadIDlist) > 0:
        print("Threads made by "+username+":")
        // we will show thread titles here in next step

Now we will modify that code block to actually set up another loop which reads through each thread ID in the list for each user.
We will then grab the thread title from our threads array so we can print out each thread title below "Threads made by ____:"
Python:
if len(threadIDlist) > 0:
        print("Threads made by "+username+":")
        for threadID in threadIDlist:
            print(threads[threadID])

Thread titles are accessed easily by referring to them numerically !

Now let's make an else statement for users that have 0 threads!
Python:
if len(threadIDlist) > 0:
        print("Threads made by "+username+":")
        for threadID in threadIDlist:
            print(threads[threadID])
else:
        print(username+" has not created any threads.")
    print("\n")

Here is the outcome of our code:
Code:
GHOST is cool.
MALCOLM is cool.
ROOT is cool.


Threads made by GHOST:
HELLO
I AM A GHOST
Do you know PHP?


Threads made by MALCOLM:
Forum Rules
WELCOME TO CF
How to use Code Forum
Where do you find stock images?

ROOT has not created any threads.

I hope these examples help you a bit, but if you need help I will be here to answer any questions!

Here is our code in FULL:
Python:
users = ["GHOST","MALCOLM","ROOT"]
for user in users:
    print(user + " is cool.")
print("\n")

data = {"GHOST":[1,3,5],"MALCOLM":[0,2,4,6], "ROOT":[]}
threads = ["Forum Rules", "HELLO", "WELCOME TO CF", "I AM A GHOST", "How to use Code Forum", "Do you know PHP?", "Where do you find stock images?"]
for username, threadIDlist in data.items():
    if len(threadIDlist) > 0:
        print("Threads made by "+username+":")
        for threadID in threadIDlist:
            print(threads[threadID])
    else:
        print(username+" has not created any threads.")
    print("\n")
 
Last edited:

New Threads

Buy us a coffee!

Back
Top Bottom