24

I have a really weird python problem. I have already asked several colleagues who all had no idea how it happened and how it could be solved.

I have a shallow dict of strings that I receive from an API call and I want to assign some of those values to a new dict.

This is what the first dict looks like. Just a bunch of strings: This is what the first dict looks like. Just a bunch of strings

I assign some values from dict1 to some keys in dict2. Really basic

    dict2={}
    dict2['access_key'] = dict1['access_key']
    dict2['secret_access_key'] = dict1['secret_access_key'],
    dict2['session_token'] =dict1['session_token'],
    dict2['region'] = dict1['region']

Then this happens. The values for "secret access key" and "session_token" are turned into tuples. "access_key" and "region" remain strings Then this happens. The values for "secret access key" and "session_token" are turned into tuples. "access_key" and "region" remain strings

I have already tried initialising the values as strings, accessing the first entry of the tuple and casting the value to a string. All of that did not change anything. It seems like the value is assigned just fine and then something weird happens that turns it into a tuple

This is a screenshot of my Interpreter settings. I am using Pyython 3.6 This is a screenshot of my Interpreter settings. I am using Pyython 3.6

I am really going crazy over this one :-/ Any help would be greatly appreciated

Christian König
  • 3,437
  • 16
  • 28
Maximilian Jesch
  • 623
  • 7
  • 16
  • 15
    Remove the commas at the end of your lines. Commas make things into tuples. – khelwood Nov 19 '18 at 07:16
  • 1
    Why do you have commas at the end of some lines where you copy over to dict2? Commas are used to define tuples in Python, could you check that part? – crazyGamer Nov 19 '18 at 07:18
  • 1
    Probably a copy/paste error where you've left commas on the end of the line. I've been coding in Python for I don't know how long, perhaps 20 years, and still very occasionally make the mistake, and I end up in the debugger trying to work out what I've done. – Peter Wood Nov 19 '18 at 07:21

3 Answers3

50

You have trailing commas at the end of two of your calls. This is why the strings are transformed to tuples.

Patol75
  • 4,342
  • 1
  • 17
  • 28
8

My dear friend , the reason you have this problem is in Python tuple is idetified by commas.

You can try this code

a = 1, 
print(a,type(a))
b = 1
print(b,type(b))

So I update your code to this:

dict2={}
dict2['access_key'] = dict1['access_key']
# please notes the end !
# dict2['secret_access_key'] = dict1['secret_access_key'],
# dict2['session_token'] =dict1['session_token'],
# above is the origin code
dict2['secret_access_key'] = dict1['secret_access_key']
dict2['session_token'] =dict1['session_token']
dict2['region'] = dict1['region']

I think this will help!

Mark White
  • 640
  • 1
  • 5
  • 12
2

Try the following by removing the trailing commas:

dict2={}
dict2['access_key'] = dict1['access_key']
dict2['secret_access_key'] = dict1['secret_access_key']
dict2['session_token'] =dict1['session_token']
dict2['region'] = dict1['region']
Nikhil Mohan
  • 118
  • 4