-1
# A tuple from a dictionary of strings
t10 = tuple({1: 'one', 2: 'two', 3: 'three'})
print(t10) # (1, 2, 3)
# A tuple from a dictionary of list of strings
t11 = tuple({1: ['red', 'blue', 'green'], 2: ['black', 'white'], 3: ['golden', 'silver']})
print(t11) # (1, 2, 3)

How can I access the values of the dict defined in the tuple? OR is it even possible?

The Thonnu
  • 3,578
  • 2
  • 8
  • 30
  • 1
    The same way you access indexes of a list? – Cow Aug 05 '22 at 10:04
  • `print(t11[index][key])`? – Nastor Aug 05 '22 at 10:04
  • @Nastor In this case index should be enough. `print(t11[0])` result `1` – Cow Aug 05 '22 at 10:06
  • I can't understand what "the dict defined in the tuple" is supposed to mean. If you want to use that dict again, then create it first and give it a name, *then* use it to create the tuple. It's the same as if I write `x = 1 + 2 + 3`, I can't access the `1 + 2` sub-result later. If I want that, I better write `y = 1 + 2` and then `x = y + 3`, then I can keep using `y`. – Karl Knechtel Aug 05 '22 at 10:14
  • Unless you wanted to make a tuple that has *one* item, where the dict *is* that item, in which case please see [How to create a tuple with only one element](https://stackoverflow.com/questions/12876177). – Karl Knechtel Aug 05 '22 at 10:19

2 Answers2

1

You want to use .items()

d = {1: 'one', 2: 'two', 3: 'three'}
t = tuple(d.items()) # ((1, 'one'), (2, 'two'), (3, 'three'))
print(t[0][0]) # 1
print(t[0][1]) # 'one'
The Thonnu
  • 3,578
  • 2
  • 8
  • 30
1

If you are trying to create a tuple containing a dict, you don't need to call tuple, you can use a tuple literal

>>> t10 = ({1: 'one', 2: 'two', 3: 'three'},) # note the trailing comma
>>> print(t10)
({1: 'one', 2: 'two', 3: 'three'},)
>>> print(t10[0][3])
three
>>>
>>> t11 = ({1: ['red', 'blue', 'green'], 2: ['black', 'white'], 3: ['golden', 'silver']},)
>>> print(t11)
({1: ['red', 'blue', 'green'], 2: ['black', 'white'], 3: ['golden', 'silver']},)
>>> print(t11[0][3])
['golden', 'silver']

The way you are currently using is using the dict as an iterable and building a tuple out of its elements. For a dict, the elements are the keys.

Holloway
  • 6,412
  • 1
  • 26
  • 33
  • 1
    It was not clear to me what the intended question is. If this answers the question, then the question is a duplicate of [How to create a tuple with only one element](https://stackoverflow.com/questions/12876177). – Karl Knechtel Aug 05 '22 at 10:20
  • That was how I read it. I'm not sure what the use for it would be though. – Holloway Aug 05 '22 at 10:21
  • Thanks. It works fine with the trailing comma. – Amit Agarwal Aug 05 '22 at 12:08
  • But here you removed the tuple keyword: t10 = tuple({1: 'one', 2: 'two', 3: 'three'},). tuple({1: 'one', 2: 'two', 3: 'three'},) and ({1: 'one', 2: 'two', 3: 'three'},) are supposed to be the same. So why does it not work when the keyword is added? – Amit Agarwal Aug 05 '22 at 12:16
  • 1
    Why are they supposed to be the same? `tuple` is a [builtin type](https://docs.python.org/3/library/stdtypes.html#typesseq-tuple) that creates a tuple from an iterable. It doesn't just create a tuple containing its arguments, eg `tuple(1, 2, 3, 4)` gives a `TypeError` instead of the tuple `(1, 2, 3, 4)` creates. – Holloway Aug 05 '22 at 12:20
  • '''t10 = tuple(({1: 'one', 2: 'two', 3: 'three'},))''' This also works now when I pout two brackets. – Amit Agarwal Aug 05 '22 at 13:02
  • 1
    When you use two brackets, you are creating a tuple literal as described in this answer, then passing that to the builtin tuple constructor. This works because tuples are iterable and it creates a new tuple containing all the elements of the first one. It is equivalent to `a = (1,2,3); b = tuple(a)`. – Holloway Aug 05 '22 at 13:20