I have a dictionary (created by plistlib) that has some values which are displayed enclosed in {}. How do I detect these values and iterate through them?
Asked
Active
Viewed 77 times
-1
-
1Possible duplicate of [In Python, how do I determine if an object is iterable?](http://stackoverflow.com/questions/1952464/in-python-how-do-i-determine-if-an-object-is-iterable) – JuniorCompressor Feb 26 '17 at 16:40
-
Do you care if it's *iterable*, or if it's *"enclosed in {}"* (a set or dictionary)? Have you considered some research? – jonrsharpe Feb 26 '17 at 16:46
-
@jonrsharpe - I am unaware even of the names of those characters, much less that their presence in python output indicated a dictionary or set. I tried searching on several search engines with 'python {}', 'python "{}"', and 'python +"{}"' and none of them returned anything useful. – Bryan Dunphy Feb 26 '17 at 20:59
-
Many search engines don't handle special characters well. Those in particular are usually called "braces", sometimes "curly brackets" or "curly braces": http://stackoverflow.com/questions/9197324/curly-braces-in-python. Sets and dictionaries will be covered in most basic Python tutorials: https://docs.python.org/2/tutorial/datastructures.html#sets – jonrsharpe Feb 26 '17 at 21:02
1 Answers
0
Values displayed enclosed in {}
like {a: b, c: d}
are called dictionaries. Values like {a, b, c}
are called sets. In your case, they are dictionaries.
dictionary = {'a': {'b', 'c'}, 'd': 1}
for key, value in dictionary.iteritems():
if isinstance(value, dict): # Check if value is dictionary
print value, "is a dictionary"
else:
print value, "is not a dictionary"

Franz Wexler
- 1,112
- 2
- 9
- 15
-
okay, I wasn't aware dictionaries could contain another dictionary inside themselves, and every search engine I tried ignored {} even if enclosed in quotes. meaning "{}". Thanks for the help. – Bryan Dunphy Feb 26 '17 at 20:23
-