-1

I need to make a single tuple out of dictionary.

So, I have the following dictionary:

dict = {'n_estimators': 300, 'min_samples_split': 10, 'min_samples_leaf': 3, 'max_features': 'auto', 'max_depth': 15, 'bootstrap': True}

I need to find a command which will return a tuple with the "=" sign instead of ":". In other words, I need to turn the keys of a dictionary to the variables and assign to them the corresponding values from dictionary.

So the output should be the following: tuple_from_dict = (n_estimators=300, min_samples_split=10, min_samples_leaf=3, max_features= 'auto', max_depth= 15, bootstrap= True)

I tried this code:

# Initialization of dictionary
dict = {'n_estimators': 300, 'min_samples_split': 10, 'min_samples_leaf': 3, 'max_features': 'auto', 'max_depth': 15, 'bootstrap': True}
 
# Converting into list of tuple
list = [(k, v) for k, v in dict.items()]
 
# Printing list of tuple
print(list)

But this code creates a tuple of tuples, separated by comma.

I also saw this question, but I am looking for the way to do it more simple. I am new to python, so please don't be very mad at a possibly stupid question.

Do you, guys, know how to fix that?

UPDATE: Based on the discussion below, it is possible to create a namedtuple out of dictionary. But is there any way to make a simple tuple? Or to get rid of a name in a namedtuple to create a simple tuple?

  • This is not a valid python. Do you want a tuple of strings with this format? – Yevhen Kuzmovych Mar 22 '23 at 13:32
  • 2
    the output you want is worng – Tanveer Ahmad Mar 22 '23 at 13:34
  • why you turn the keys into assignment? – Tanveer Ahmad Mar 22 '23 at 13:35
  • I was just wondering if that is possible. Again, I am new to python, and I am exploring if I can do that with dictionaries. – PythonLover Mar 22 '23 at 13:37
  • sorry, no, you cant but you can go with the dictionary or you go with list, basically tuple is unchangeable – Tanveer Ahmad Mar 22 '23 at 13:40
  • (('n_estimators', 300), ('min_samples_split', 10), ('min_samples_leaf', 3), ('max_features', 'auto'), ('max_depth', 15), ('bootstrap', True)), you can go with this output with converting single list into tuple like this: tuple(list) – Tanveer Ahmad Mar 22 '23 at 13:44
  • @MSpiller, the question works pretty good! But now I get the output: namespace(n_estimators=300, min_samples_split=10, min_samples_leaf=3, max_features='auto', max_depth=15, bootstrap=True) and I don't know how to get rid of "namespace". – PythonLover Mar 22 '23 at 13:46
  • If you just want the things represented as a string in the format you require `(key1=value1, key2=value2, ...)`, then you'll need to create a new class and specific defined [`__str__`](https://docs.python.org/3/reference/datamodel.html#object.__str__) and `__repr__` methods that give you the output you want. – Matt Pitkin Mar 22 '23 at 14:07

1 Answers1

0

Maybe you're thinking of a named tuple which you could construct as follows:

from collections import namedtuple

_dict = {'n_estimators': 300, 'min_samples_split': 10, 'min_samples_leaf': 3, 'max_features': 'auto', 'max_depth': 15, 'bootstrap': True}

FOO = namedtuple('FOO', _dict)

print(FOO(*_dict.values()))

Output:

FOO(n_estimators=300, min_samples_split=10, min_samples_leaf=3, max_features='auto', max_depth=15, bootstrap=True)
DarkKnight
  • 19,739
  • 3
  • 6
  • 22