Python

Nemphis

Advanced OT User
Joined
Jun 22, 2009
Messages
325
Reaction score
114
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?
 

Stigma

Veteran OT User
Joined
Feb 14, 2015
Messages
4,554
Reaction score
2,102
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)
 
OP
Nemphis

Nemphis

Advanced OT User
Joined
Jun 22, 2009
Messages
325
Reaction score
114
Location
Sweden
thank you. it helped more than most sites
 
Top