user2052567
user2052567

Reputation: 439

How to create and fill 2d list in python. Basically trying to create a list of rgb values

What I'm trying to do is allow the user to enter in the red, green, blue of there color and how many tints they want and saving that into a 2d list. Right now I am just trying to put their rgb value in the list the same number of times they enter for the tint.

So for example they enter r = 255 g = 0 b = 0 numTint = 5

I want to fill the list so it is like this for now: tintList = [(255,0,0), (255,0,0), (255,0,0), (255,0,0), (255,0,0)]

The problem is I'm am new to programming an python so I'm am not quite sure how to do this. I believe you have to use nested loops, but I am not sure how. I would really appreciate any help I can get.

def createColorList(r,g,b, numTint):
    tintList = []
    #fill tintList

    return tintList

Upvotes: 0

Views: 383

Answers (2)

Gimo
Gimo

Reputation: 178

def createColorList(r,g,b, numTint):
    tintList = []
    for x in range(0, numTint):
        tintList.append((r,g,b))
    return tintList

But I think Use a class to represent RGB value, maybe a better practice.

class Color(object):
    def __init__(self, red, green, blue, tint):
        self.red = red
        self.green = green
        self.blue = blue
        self.tint = tint

    def somemethod(self):
        pass #you can add some method.

Upvotes: 1

Padraic Cunningham
Padraic Cunningham

Reputation: 180411

Just use a list comprehension and range:

return [(r,g,b) for _ in range(numTint)]


In [25]: createColorList(255,0,0,5)
Out[25]: [(255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0)]

Upvotes: 0

Related Questions