Reputation: 1725
I have a list of tuple of key index, color code, and color name as like this:
BG_MENU_THEME = [
('1','#031B4D','Blue Ocean'),
('2','#303E4D','Grayish blue'),
('3','#062847','Ghibli Ocean Drak 1'),
('4','#00122e','Ghibli Ocean Drak 2'),
('4','#00122e','Ghibli Ocean Drak 2'),
('5','#115478','Ghibli Ocean - Ligit'),
('6','#243447','Twitterish Color'),
('7','#152324','Dark Forrest'),
('8','#11202F','Dark Blue Original'),
('99','','_None')
]
Given colorIndext='8'
, how can I get color code '#11202F'
? not to say BG_MENU_THEME[8][1]
because it will raise an error out of index
if colorIndex='99'
.
I tried:
BackgroundColor = BG_MENU_THEME[row[0]] for row in BG_MENU_THEME if row[0]==ColorIndex
and got SyntaxError: invalid syntax
Please kindly help advise me on this ? Thanks
Upvotes: 0
Views: 407
Reputation: 26315
Using a dictionary also works:
BG_MENU_THEME = [
('1','#031B4D','Blue Ocean'),
('2','#303E4D','Grayish blue'),
('3','#062847','Ghibli Ocean Drak 1'),
('4','#00122e','Ghibli Ocean Drak 2'),
('4','#00122e','Ghibli Ocean Drak 2'),
('5','#115478','Ghibli Ocean - Ligit'),
('6','#243447','Twitterish Color'),
('7','#152324','Dark Forrest'),
('8','#11202F','Dark Blue Original'),
('99','','_None')
]
d = {key: code for key, code, color in BG_MENU_THEME}
BackgroundColor = d['8']
print(BackgroundColor)
# #11202F
You could also keep BG_MENU_THEME
as a dictionary beforehand, then no transformation stage is needed.
Upvotes: 1
Reputation: 5713
Try this code:
BG_MENU_THEME = [
('1','#031B4D','Blue Ocean'),
('2','#303E4D','Grayish blue'),
('3','#062847','Ghibli Ocean Drak 1'),
('4','#00122e','Ghibli Ocean Drak 2'),
('4','#00122e','Ghibli Ocean Drak 2'),
('5','#115478','Ghibli Ocean - Ligit'),
('6','#243447','Twitterish Color'),
('7','#152324','Dark Forrest'),
('8','#11202F','Dark Blue Original'),
('99','','_None')
]
colorId = '8'
result = None
for data in BG_MENU_THEME:
if data[0] == colorId:
result = data[1]
if result:
print(result)
else:
print(BG_MENU_THEME[-1][1])
Upvotes: 0
Reputation: 911
BG_MENU_THEME = [
('1','#031B4D','Blue Ocean'),
('2','#303E4D','Grayish blue'),
('3','#062847','Ghibli Ocean Drak 1'),
('4','#00122e','Ghibli Ocean Drak 2'),
('4','#00122e','Ghibli Ocean Drak 2'),
('5','#115478','Ghibli Ocean - Ligit'),
('6','#243447','Twitterish Color'),
('7','#152324','Dark Forrest'),
('8','#11202F','Dark Blue Original'),
('99','','_None')
]
BackgroundColor = '' # give it a default value
ColorIndex = '8'
res = [row[1] for row in BG_MENU_THEME if row[0]==ColorIndex and row[1]]
if len(res) == 1:
BackgroundColor = res.pop()
print(BackgroundColor)
# #11202F
Upvotes: 0
Reputation: 27802
Use a generator with next
:
BackgroundColor = next(color[1] for color in BG_MENU_THEME if color[0] == colorIndex)
Upvotes: 3
Reputation: 437
for tup in BG_MENU_THEME:
if tup[0] == "8":
print (tup[1]) # background color.
or the short way: [tup[1] for tup in BG_MENU_THEME if tup[0] == "8"][0]
if you really want to use list comprehension. You are using the syntax for a list comprehension when you are not actually using list comprehension. That's why it's raising an error.
Iterate through each tuple and check if first value in tuple is "8"
. If it is print the second value of that tuple.
Upvotes: 0