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
  • 112,593
  • 23
  • 157
  • 280
  • 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,197
  • 8
  • 21
2
>>> print(' '.join(*a[1].values()))
a = c
Dishin H Goyani
  • 6,501
  • 3
  • 23
  • 34
2

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

print(*a[1]['c'])
blhsing
  • 77,832
  • 6
  • 59
  • 90
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
  • 4,552
  • 3
  • 18
  • 30