0

As I already asked on (Convert dictionary key tuple to string), but this time I have a dict={("name1","name2","name3"):256}

I want my dict to be dict={"name1,name2,name3":256} but when I try this I get an error:

    new_dct = {}
    for k,v in dicts.items():
        new_dct[','.join(k)] = v
**TypeError: sequence item 1: expected str instance, float found** 

(i removed all nanS) Any idea?

5
  • 1
    it can't raise that error with that input Commented Jul 23, 2022 at 20:00
  • @Matiiss That's not correct. With dicts={(1.0, "name1"):256} it would raise that exception (plus or minus the item number), and OP has not shown how dicts gets assigned. Commented Jul 23, 2022 at 20:03
  • @constantstranger yes, it is correct, they say that they have a dict = {("name1", "name2", "name3"): 256} which is their provided sample input apparently and with it, that error gets raised, that's false, with that input and that code, that error can't be raised, you basically said the same thing in your answer (questionable why it's even an answer, but sure) Commented Jul 23, 2022 at 20:05
  • My point is that the code uses dicts (not dict), and we're not shown what's in dicts. Commented Jul 23, 2022 at 20:07
  • @Matiss to put a finer point on the matter: whatever input OP is using in dict, because the code references the variable dicts whose value we are not shown, the exception shown in OP's question is entirely possible. Commented Jul 23, 2022 at 20:22

3 Answers 3

3

Try this:

d = {("name1", "name2", "name3"): 256}
new_d = {','.join(map(str, k)): v for k, v in d.items()}
print(new_d) # {'name1,name2,name3': 256}
Sign up to request clarification or add additional context in comments.

Comments

0

I tried your code with dict renamed as d and used in the loop in place of dicts (the value of which is unclear), and it seems to work fine:

d={("name1","name2","name3"):256}

new_dct = {}
for k,v in d.items():
    new_dct[','.join(k)] = v

print(new_dct)

Output:

{'name1,name2,name3': 256}

Here are a few notes:

  • In the code in your question, you use dicts without showing what value you assigned to it.
  • If (for example) you had initialized dicts using dicts={("name1", 2.0, "name3"):256}, it would raise the exception you have mentioned (TypeError: sequence item 1: expected str instance, float found).
  • You may want to examine the contents of the dictionary that you iterate over just before the loop to ensure the items in the tuple keys are all of type str, otherwise join() may not work.

2 Comments

did you just post their "problematic" code as an answer?
@Matiss Thanks for your question. I posted working code similar to that of OP but with a clearly initialized input variable d in place of the variable dicts with unknown value (and also without using the python type name dict as a variable name).
0

It's the same like before in Convert dictionary key tuple to string. Just remove 's' in dicts.items() as in your first dict variable or find another typo in your code espescially in element of dict or dicts. The exception (TypeError) could be cause by different reference to your dict or dicts variable. Others' answers already solved it, whether using dict comprehension:

dict = {("name1","name2","name3"):256}
new_dct = {','.join(key): value for key, value in dict.items()}
print(new_dct)

or normal for loop:

dict = {("name1","name2","name3"):256}
new_dct = {}
for key, value in dict.items():
    new_dct[','.join(key)] = value
print(new_dct)

and the result will be same output.

{'name1,name2,name3': 256}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.