0

I have litterly read every post I could find on pulling keys from this array. I can get the the array to print the dic but i cant seem to pull the keys. I need it to print first and last name. Learning python but cant seem to get this.

students = [
     {'first_name':  'Michael', 'last_name' : 'Jordan'},
     {'first_name' : 'John', 'last_name' : 'Rosales'},
     {'first_name' : 'Mark', 'last_name' : 'Guillen'},
     {'first_name' : 'KB', 'last_name' : 'Tonel'}
]


for i in students:


    first = [students[i].get('first_name') in students.values()] + [students[i].get('last_name') in students.values()]
    second= [students[i].get('first_name') in students.values()] + [students[i].get('last_name') in students.values()]
    third=  [students[i].get('first_name') in students.values()] + [students[i].get('last_name') in students.values()]
    four=   [students[i].get('first_name') in students.values()] + [students[i].get('last_name') in students.values()]



    print first, second, third, fourth
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
David Hollenbeck
  • 121
  • 1
  • 1
  • 13

2 Answers2

0

While looping through array, your i is the element already

for i in students:
  print([i['first_name'], i['last_name']])
Đào Minh Hạt
  • 2,742
  • 16
  • 20
  • This works but i cant post a yes for 7 min. Is there a way to make it just post the names and not the ['Michael', 'Jordan'] ['John', 'Rosales'] ['Mark', 'Guillen'] ['KB', 'Tonel'] – David Hollenbeck Mar 07 '17 at 03:29
  • @DavidHollenbeck That is a separate question. Please ask a new question instead (or better yet, search for a previously asked one because it has for sure been answered before, like [here](http://stackoverflow.com/questions/15769246/pythonic-way-to-print-list-items)). – SethMMorton Mar 07 '17 at 03:31
  • Yes it can replace to `print "{} {}\n".format([i['first_name'], i['last_name'])` – Đào Minh Hạt Mar 07 '17 at 03:38
0

If I understand this correctly, it looks like you just want to store the first and last name in the string respectively.

students = [
    {'first_name':  'Michael', 'last_name' : 'Jordan'},
    {'first_name' : 'John', 'last_name' : 'Rosales'},
    {'first_name' : 'Mark', 'last_name' : 'Guillen'},
    {'first_name' : 'KB', 'last_name' : 'Tonel'}
]
first = students[0]["first_name"] + " " + students[0]["last_name"]
second = students[1]["first_name"] + " " + students[1]["last_name"]
third = students[2]["first_name"] + " " + students[2]["last_name"]
fourth = students[2]["first_name"] + " " + students[2]["last_name"]
print first, second, third, fourth

and gives the output

Michael Jordan John Rosales Mark Guillen Mark Guillen

This will store the first and last name of the values of the dictionary within the variable.

I had to use list notation to get the index of the dictionary that I wanted then access the value with the key. If you look at the first line. students[0]["first_name"] The array notation [0] is referring to {'first_name': 'Michael', 'last_name' : 'Jordan'} this dictionary. The ["first_name"] allows me to access the key_value sorted at the key. in our case 'Michael'.

richard_d_sim
  • 793
  • 2
  • 10
  • 23