isinstance(x, list) when iterating a list containing strings and lists
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT
Music by Eric Matyas
https://www.soundimage.org
Track title: Book End
--
Chapters
00:00 Isinstance(X, List) When Iterating A List Containing Strings And Lists
00:47 Accepted Answer Score 7
01:39 Thank you
--
Full question
https://stackoverflow.com/questions/1496...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #collections #iterable
#avk47
ACCEPTED ANSWER
Score 7
I think that your answer is OK here -- Although I might change it to
if not isinstance(x,basestring):
...
to make it a little more accepting depending on the expected input. Ultimately, some problems you need isinstance which is why it still exists in the language.
That being said, the thing about this problem that is un-pythonic is the data-structure. In python, if a problem seems really hard, it probably means you're storing the data in the wrong way ... (Of course, I realize that you had no control over what the original poster of the other question's data structure was ;-).
Basically, I guess that my view is that isinstance is the hack that you need to make lemonade when your colleagues/some library writer somewhere gives you lemons -- Otherwise you avoid it where possible.