Question

I have 2 lists as below:

bob = ['Bob Smith', 42, 30000, 'software']
sue = ['Sue Jones', 45, 40000, 'hardware']

the statement

print bob[0].split()[-1]

gives me Smith

I wanted to get more information regarding list therefore I used 2 commands:

help(list)
dir(list)

But neither showed that split is a method or object that can be used by lists. But as you can see above I can use split() with list element.

Can someone help me in this naive question. Am I looking at wrong places?

Regards

Was it helpful?

Solution 2

If you're looking for a string type, you can do this:

if type(bob[0]) is str:
    bob[0].split()[-1]

str type always have the split() method.

OTHER TIPS

Since a method is a kind of attribute, you can use hasattr

[hasattr(item, 'split') for item in bob]

will give you [True, False, False, True]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top