1
a=[{"a":30},{"c":("a","=","c")}]

I need values of "c".

for x in a[1].values():
    for j in x:
        print(j, end=" ")

This is my solution but if you have any other short solution for this then tell me.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    What's wrong with `a[1]['c']` or even `print(*(_ for _ in a[1]['c']))`? – accdias Jan 02 '20 at 17:55
  • What is the question? You're providing an example but does it do what you expect? If yes, why do you need another solution? If not, what do you expect? If I take your actual question literally, the answer is: `"c"`. That's "the value of `"c"`". – pasbi Jan 02 '20 at 18:17
  • 1
    For those like me who didn't know what the * was, look for "tuple unpacking", or other uses at https://stackoverflow.com/questions/2921847/what-does-the-star-operator-mean-in-a-function-call – B. Go Jan 02 '20 at 18:21

4 Answers4

2

You could simply say

for element in a[1]['c']:
       print(element,end=" ")
Swetank Poddar
  • 1,257
  • 9
  • 23
2
>>> print(' '.join(*a[1].values()))
a = c
Dishin H Goyani
  • 7,195
  • 3
  • 26
  • 37
2

If you just need the values to be printed, you can unpack the tuple as arguments to print:

print(*a[1]['c'])
blhsing
  • 91,368
  • 6
  • 71
  • 106
1

The same result can be achieved with a comprehension:

>>> a=[{"a":30},{"c":("a","=","c")}]
>>> print(*(_ for _ in a[1]['c']))
a = c
>>> 
accdias
  • 5,160
  • 3
  • 19
  • 31