my_roster_dict = {'p': 'clayton kershaw',
'rf': 'mookie betts',
'1b': 'cody bellinger'}
How would it be possible only print items with the last name of b? I've tried utilizing split, but end up with tuple object error.
CodePudding user response:
Just use a for loop over the key,value pair from the items()
view and split the value by the space and check the first character. There is nothing wrong with sticking to the basics.
for k,v in my_roster_dict.items():
first, last = v.split()
if last[0].lower() == "b":
print(k,v)
CodePudding user response:
print((my_roster_dict['1b'].split())[-1])
my_roster_dict['1b'] --> you get the value in a string type;
.split() ---> transform it into a list which [0] is the first name [1] is the last name. in case there is a middle name, use [-1] to get the last name.