How to get Value out of list of tuple in python?

python create list of tuples
tuple inside list python
python tuple vs list
tuples python
get first element of tuple python
python list
get second element of tuple python
python get nth element of list of lists

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


Use a generator with next:

BackgroundColor = next(color[1] for color in BG_MENU_THEME if color[0] == colorIndex)

Find an element in a list of tuples, Find an element in a list of tuples · python list search tuples. I have a list 'a' a= [(  The computation of min and max value is a quite common utility in any programming domain be it development or any other field that includes any programming constructs. Sometimes, data can come in the format of tuples and min and max operations have to be performed in them. Let’s discuss certain ways in which this is handled.


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.

Lists and Tuples in Python – Real Python, You'll cover the important characteristics of lists and tuples in Python 3. You have seen that an element in a list can be any sort of object. You can delete multiple elements out of the middle of a list by assigning the appropriate slice to an  Each of the key value pair becomes a tuple. So the final list is a list whose elements are tuples. With items() We sue the items method of the dictionary which allows us to iterate through each of the key value pairs. Then we use a for loop to pack those values in to a tuple. We put all these tuples to a final list. Example. Live Demo


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.

Python Lists of Tuples, Both lists and tuples in Python are handy elements to use for structuring data. Take a look at these instructional examples of how to use each of them. With tuples we can "unpack" the values like this: In [16]: belkas, gorkin, Check out the Python reference documentation for more info on tuples. Now that  Python Tuples. Python provides another type that is an ordered collection of objects, called a tuple. Pronunciation varies depending on whom you ask. Some pronounce it as though it were spelled “too-ple” (rhyming with “Mott the Hoople”), and others as though it were spelled “tup-ple” (rhyming with “supple”).


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

Python - Tuples, The differences between tuples and lists are, the tuples cannot be ch. To write a tuple containing a single value you have to include a comma, even though  Given a list of tuples, write a Python program to sort the tuples by the second item of each tuple. Using the technique of Bubble Sort to we can perform the sorting. Note that each tuple is an element in the given list. Access the second element of each tuple using the nested loops. This performs the in-place method of sorting.


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])

Tuples, Syntactically, a tuple is a comma-separated list of values: To create a tuple with a single element, you have to include the final comma: Without the comma Python treats ('a') as an expression with a string in parentheses that evaluates to a As you should expect from a dictionary, the items are in no particular order. The most conventional and pythonic way to perform this particular task is using this function. This collects all the nth tuple elements into a single container and returns the result. This only works for Python 2. # Python code to demonstrate. # get nth tuple element from list. # initializing list of tuples. test_list = [ (1, 'Rash', 21), (2


Lists and Tuples, Loops are not effective when you are trying to generate different output for each value in your list. Your First  The empty tuple is written as two parentheses containing nothing − tup1 = (); To write a tuple containing a single value you have to include a comma, even though there is only one value − tup1 = (50,); Like string indices, tuple indices start at 0, and they can be sliced, concatenated, and so on. Accessing Values in Tuples


9. Tuples, The index operator selects an element from a tuple. we have to include the final comma, because without the final comma, Python treats the (5) a list of pairs, and we had an example where one of the items in the tuple was itself a list:. Create Tuple With One Item. To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple.


20. Dictionaries, Python uses complex algorithms, designed for very fast access, to determine where Lists, tuples, and strings have been called sequences, because their items occur in order. The del statement removes a key:value pair from a dictionary. Given a dictionary, write a Python program to convert the given dictionary into list of tuples. Below are various methods to convert dictionary to list of tuples. Check out this Author's contributed articles. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your