0

I have a dictionary that has multiple keys and each key has multiple values. However the values in each key are syntaxed with brackets which makes the program think each key is a list of values.

d = {'Select Income REIT': ['SIR', '25.19', '25.41', '25.13', '25.25', '0.16', '0.64', '330625', '25.57', '17.07', '2', '7.92', '21.73', '27.4'], 'Mercury General': ['MCY', '51.82', '52.41', '51.78', '52.37', '1.04', '2.03', '123768', '57.19', '42.97', '2.48', '4.74', '40.6', '12.45']}

How do I write code to remove the [] from this line? I know i can just delete it, but I have thousands of dictionary keys that have this syntax.

Thank you!

Deuce525
  • 77
  • 1
  • 2
  • 10
  • 2
    If you deleted the brackets, you would no longer have valid Python. What exactly are you trying to do? – user94559 Sep 01 '16 at 18:22
  • 1
    BTW, it's the *values* that are lists, not the keys. In the above dictionary, the keys are strings. – user94559 Sep 01 '16 at 18:22
  • 1
    "I know I can just delete it, but I have thousands of dictionary keys." Where is this dict anyway? Are you talking about "just deleting" this programmatically or is it written in a file/your editor somewhere? This is very confusing to me. – Two-Bit Alchemist Sep 01 '16 at 18:24
  • ha okay bare with me guys, i'm pretty new at Python but my goal is to use the function d = dict((k,int(v)) for k,v in d.iteritems()) but its returning a error of TypeError: int() argument must be a string or a number, not 'list' so i thought it was treating the values in the key as a list (?). If its supposed to be treated as a list, why the error? – Deuce525 Sep 01 '16 at 19:08
  • @Deuce525: Dictionaries associate a key with _one single_ value. In this example, the key `Select Income REIT` is associated with a single value, and that value is a list of string representations of numbers. If you delete the brackets to remove the list, you'll be trying to associate `Select Income REIT` with multiple values, which is not possible. I think what you're trying to do is to cast each number in the list to an `int`. For this, I recommend using the `map` function, for example `d = {k : map(lambda x: int(x), v)}`. – Samira N Sep 01 '16 at 20:11
  • Isn't the following example d = {'beta': ['ABC', '1', '5', '10', '15'], a dictionary that has a key with multiple values? – Deuce525 Sep 01 '16 at 20:27
  • No, it's a key that's associated with a single value, and that value happens to be a list. To clarify the distinction, `{"beta" : [1, 2, 3]}` is a valid dictionary, but `{"beta" : 1, 2, 3}` is not. I'm sorry if that sounds pedantic, but it's relevant to your question: you asked how to remove the brackets from your dictionary's values so that Python would stop treating each value as a list. My point is that each value _has_ to be a list - it can't just be several numbers all associated with the same key. – Samira N Sep 01 '16 at 20:50
  • @SamiraN Why create a special lambda just to duplicate `int`? Just pass `int`. All you are doing is making `map` take longer. – Two-Bit Alchemist Sep 02 '16 at 02:02

0 Answers0