user12166681
user12166681

Reputation:

How to put into a tuple?

I have a dictionary of :

dict1 = {
  1: {'red': 5, 'blue': 7}, 
  2: {'red': 2, 'blue': 6, 'yellow': 9}, 
  3: {'red': 8, 'yellow': 4}
}

How would I print:

((1,2)(2,3)(3,2))

with the keys being the first key terms in dict1, and the value is how many colors in each?

Upvotes: 0

Views: 73

Answers (2)

RoadRunner
RoadRunner

Reputation: 26315

You need collect the key and length of value(dictionary) into a tuple of tuples:

>>> tuple((k, len(v)) for k, v in dict1.items())
((1, 2), (2, 3), (3, 2))

You can iterate the key and value of a dictionary with dict.items()

Upvotes: 2

Selcuk
Selcuk

Reputation: 59184

You can use zip as follows:

>>> tuple(zip(dict1.keys(), map(len, dict1.values())))
((1, 2), (2, 3), (3, 2))

Upvotes: 2

Related Questions