-1
dict.setdefault('Name','').str('Name 1')

I want to append 'Name' to the dictionary directly in a for loop and if the name is not available, I want it to be an empty string. Can we get this using setdefault?

When I use the above code, I get error as -

AttributeError: 'str' object has no attribute 'str'
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Bharat Bittu
  • 525
  • 1
  • 9
  • 26
  • I'm not sure what you're trying to do. `setdefault` will return a string, as the error message suggests; the string is the new value in the dictionary. What are you trying to do with the string? – APerson Jul 05 '18 at 20:07
  • 1
    Given that `.setdefault` either returns the existing value or the default `''`, what did you expect from `.str`? – jonrsharpe Jul 05 '18 at 20:07
  • @jonrsharpe @APerson This is the default method `setdefault(key,[ ]).append(value)` But instead of list, I want a string. – Bharat Bittu Jul 05 '18 at 20:11
  • 1
    Sure, but that example isn't calling `.list`. It's calling `.append` **on** a list. So why access `.str`, an attribute strings definitely don't have? – jonrsharpe Jul 05 '18 at 20:12
  • @jonrsharpe Well, I was hoping for default to be an empty string instead of a list. That's why. – Bharat Bittu Jul 05 '18 at 20:15
  • 1
    Yes, I understand that, but **the default is the second argument to `setdefault`**, per [the docs](https://docs.python.org/3/library/stdtypes.html#dict.setdefault). Exactly like the example you have, where the default is an empty list *passed as the second argument*. So `.setdefault('Name', '')` achieves what you say what you want, and it's not clear to me at all why you've tacked `.str('Name 1')` on to that. The error message is pretty clear: strings don't have a `str` method. – jonrsharpe Jul 05 '18 at 20:17

2 Answers2

1

You cannot do what you are looking to do because str is immutable, you will always need to construct a new string and assign it back to the key. You can do this with lists because the list is mutable, i.e. you can mutate the list in the dictionary.

For str just use dict.get(). This will append the value onto the existing value in the dictionary if it exists or to the empty string if it doesn't, e.g.:

d['Name'] = d.get('Name', '') + 'Name 1'

Alternatively, use a collection.defaultdict, e.g.:

d = defaultdict(str)
d['Name'] += 'Name 1'

Note: don't use dict as a variable name it hides pythons dict type.

AChampion
  • 29,683
  • 4
  • 59
  • 75
0

I want to append 'Name' to the dictionary directly in a for loop and if the name is not available, I want it to be an empty string

This can be done with just setdefault:

dict.setdefault('Name', '')

Note: dict is a very bad name for a variable: it shadows the built-in type dict.

Eugene Primako
  • 2,767
  • 9
  • 26
  • 35
  • 'Name' is the key here. How can we assign a value to key? – Bharat Bittu Jul 05 '18 at 20:09
  • @BharatBittu There is no direct assignment in my example. See the link to docs that i've posted: "If key is in the dictionary, return its value. If not, insert key with a value of default and return default.". Here default is ```""``` (empty string) – Eugene Primako Jul 05 '18 at 20:12