• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Python

Nemphis

Veteran OT User
Joined
Jun 22, 2009
Messages
653
Reaction score
393
Location
Sweden
I apologise for asking, but i started Learning Python and i need to do a disctionary/list -thing like we do in LUA. But i cant seem to find out how to do it.

What i want it something similiar to this.
Code:
import random

input = random.randint(1, 2)

list = {
    [1] = {first = 1, second = 2, third = 3, forth = 4}
    [2] = {first = 10, second = 20, third = 30, forth = 40}
}

theList = list[input]

I've searched on Youtube and Learning websites, but i cant seem to find any.

Anyone knows on how I can do it in Python 3.x?
 
Solution
Avoid naming your variables things like "input" and "list", those are both python functions and you are overriding them.
Keep in mind lists start from 0 not 1.
Python:
import random

selection = [
    {'first': 1, 'second': 2, 'third': 3, 'forth': 4},
    {'first': 10, 'second': 20, 'third': 30, 'forth': 40}
]

selected = random.choice(selection) # or selection[random.randint(0, len(selection) - 1)]

print(selected)
Avoid naming your variables things like "input" and "list", those are both python functions and you are overriding them.
Keep in mind lists start from 0 not 1.
Python:
import random

selection = [
    {'first': 1, 'second': 2, 'third': 3, 'forth': 4},
    {'first': 10, 'second': 20, 'third': 30, 'forth': 40}
]

selected = random.choice(selection) # or selection[random.randint(0, len(selection) - 1)]

print(selected)
 
Solution
Back
Top