How to check if the first element of each tuple inside a list is a key of a dictionary?

Viewed 545

For instance, if there's a dictionary student_gpa where:

student_gpa = {'Alex': 3.00, 
               'John': 3.50,
               'Jennifer': 3.75}

and a list of tuples student_age where:

student_age = [('Mike', 14),
              ('Alex', 13),
              ('John', 15)]

How do I check if 'Mike', 'Alex', and 'John' are all keys inside the dictionary student_gpa?

7 Answers

To check if an object is a key in a dict you can call dict.keys() which will return a list of all keys in the dict.

name in dict.keys()

However, if you just want to check that all your students exist in the dict you can use all() and a loop to determine if all names are found within the list of keys.

>>> all(student in student_gpa.keys() for student, age in student_age)
#False

>>> student_gpa['Mike'] = 1.0
>>> all(student in student_gpa.keys() for student, age in student_age)
#True

You could loop through the list, checking the first element of each tuple. Then check if that value is a key:

for i in student_age:
    if i[0] in student_gpa.keys():
        print("yes")`

Using student_gpa.keys() returns a view object of the keys in student_gpa. A view object is an iterable. Here is a nice article explaining .keys().

Or you could try using zip, and try using a generator with all, like below:

print(all(i in student_gpa.keys() for i in list(zip(*student_age))[0])

Output:

False

@PacketLoss's answer is good too:

print(all(x in student_gpa.keys() for x, y in student_age))

Output:

False

Here is something that worked for me

tuple_list = [("a", "b"),("c", "d")]
first_tuple_elements = [a_tuple[0] for a_tuple in tuple_list]#Getting all tupples first element
student_gpa = {'a': 3.00, 
               'c': 3.50}
for key in first_tuple_elements:
    if key in list(student_gpa.keys()):
        print(key)

Output:

a
c

To check for if all of the first element of list of tuple are key in the dictionary

print(first_tuple_elements in list(student_gpa.keys()))

Output:

True

To check if all the keys of of dic are present in a list of tuple and vice versa

print(list(student_gpa.keys())==first_tuple_elements)

Output:

True

You can do it with loop.

student_gpa = {'Alex': 3.00,
                   'John': 3.50,
                   'Jennifer': 3.75}
student_age = [('Mike', 14),
                  ('Alex', 13),
                  ('John', 15)]
student_gpa_keys = list(student_gpa.keys())
list_keys = [item[0] for item in student_age]
    
for ele in list_keys:
    if ele in student_gpa_keys:
        print(ele)

result:

Alex
John

I don't understand why some answers are so fixed on using single line code. IMHO, the below is clean and easy to understand, especially for a beginner.

for student, age in student_age:
    if student in student_gpa: # no need to use student_gpa.keys() to check if a key exists
        print(student, 'is in the dictionary')
        # Do something
    else:
        print(student, 'is not in the dictionary')
        # Do something else

You can do this using list comprehension.

student_gpa = {'Alex': 3.00, 
               'John': 3.50,
               'Jennifer': 3.75}
student_age = [('Mike', 14),
              ('Alex', 13),
              ('John', 15)]

students = [std for std, _ in student_age if std in student_gpa]

print (students)
Related