0

I am trying to extract elements from a list of tuples. I got the answer but wanted to know is there a better way? code:

pred_result_tuple  = [('n02123045', 'tabby', 0.5681726)]
pred_result_list = [x[n] for x in pred_result_tuple for n in [0,1,2]]

print(pred_result_list)
['n02123045', 'tabby', 0.5681726]

Tried but failed:

print([x[n] for x,n in zip(pred_result_tuple,[0,1,2])])
['n02123045']
Mainland
  • 4,110
  • 3
  • 25
  • 56
  • 3
    does your list always have just one tuple in it? or you want to extract all the elements from all the tuples in the list into a single flat list? – Anentropic Jun 18 '22 at 16:41
  • 1
    From what you posted, `list(pred_result_tuple[0])` seems enough. – j1-lee Jun 18 '22 at 16:44
  • If you want something like a flattening operation, see this answer: https://stackoverflow.com/a/953097/7661119 – Ervin Szilagyi Jun 18 '22 at 16:45
  • @Anentropic I want to give a bit of context. This is the prediction result of a neural network. This is the top prediction. Right now I am looking only at the top-1 result, which gives one tuple. Yes! may be I will consider top-3 results, which gives a list of three tuples. – Mainland Jun 18 '22 at 16:46
  • 1
    but if you select the top three results presumably you want to keep them separate. I don't really see the point in most of this, why not just get the first tuple out as a tuple `pred_result = pred_result_tuples[0]; print(pred_result)` – Anentropic Jun 18 '22 at 16:48
  • @Anentropic Yes! this seems to be the easiest answer I am looking for. – Mainland Jun 18 '22 at 16:51
  • @Anentropic here is answer when I want my neural model to produce top-3 predictions for a given image: `[('n02123045', 'tabby', 0.5681726), ('n02124075', 'Egyptian_cat', 0.22223175), ('n02123159', 'tiger_cat', 0.061033934)]` – Mainland Jun 18 '22 at 16:52
  • So for the top-3 predictions, you want to flat `[('n02123045', 'tabby', 0.5681726), ('n02124075', 'Egyptian_cat', 0.22223175), ('n02123159', 'tiger_cat', 0.061033934)]` as `['n02123045', 'tabby', 0.5681726, 'n02124075', 'Egyptian_cat', 0.22223175, 'n02123159', 'tiger_cat', 0.061033934]`? – Haoliang Jun 18 '22 at 16:54
  • @Haoliang @Anentropic Here is an interesting thing. Why do I want this? I wanted to know index the position of each predicted class (in this case `tabby`) in the trained model list, which is here `https://storage.googleapis.com/download.tensorflow.org/data/imagenet_class_index.json` I am importing this URL as a dataframe and then I am comparing the predicted class name with the dataframe to know its index position. – Mainland Jun 18 '22 at 17:01
  • You could just do `pred_result_tuples[0]`, and get tuple of the three predicted classes from the top result. You can then enumerate over the tuple to compare index positions with the dataframe. You should add the dataframe stuff to the question because what you've asked for (listify a tuple) is pointless and the answers so far probably aren't useful to you – Anentropic Jun 18 '22 at 17:58
  • @Mainland well, I am still not getting it. Let's say we want to get the top `i` predicted class name, with the list of tuples, we can use `pred_result_tuple[i][1]` to get the predicted class name (if `i == 0`, then we get `pred_result_tuple[0][1]` which is `tabby`.) In comparison, with the flattened list, we need to use `pred_result_list[i * 3 + 1]` to get the predicted class name. – Haoliang Jun 18 '22 at 18:12

2 Answers2

2
import itertools
result = list(itertools.chain(*pred_result_tuple))

>>> print(result)
>>> ['n02123045', 'tabby', 0.5681726]

But, if you have only one tuple inside the list, you can do:

result = list(*pred_result_tuple)

>>> print(result)
>>> ['n02123045', 'tabby', 0.5681726]
BhusalC_Bipin
  • 801
  • 3
  • 12
1

Use

Listoftuples=[(#elements)]
l=[]
for e in Listoftuples[0]:
    l=l+[e]
print(l)

Result

[#elements]
Supergamer
  • 411
  • 4
  • 13
  • It can be simplified as `l = list(Listoftuples[0])`. And it only works if the list contains one tuple. – Haoliang Jun 18 '22 at 16:59