access first element of dictionary python
res = next(iter(test_dict))
access first element of dictionary python
res = next(iter(test_dict))
take first n row of dictionary python
a_dictionary = {"name" : "John", "age" : 35, "height" : 65}
dict_items = a_dictionary.items()
first_two = list(dict_items)[:2]
print(first_two)
OUTPUT
[('name', 'John'), ('age', 35)]
first_three = list(dict_items)[:3]
print(first_three)
OUTPUT
[('name', 'John'), ('age', 35), ('height', 65)]
python get first n elements of dict
"""
NOTE: dictionaries do NOT remember the order in which
key : value pairs have been stored
"""
d = {"a" : 5, "b" : 1, "c" : 8, "d" : 4, "e" : 3, "f" : 10, "g": 2}
# getting the first n key : value pairs
n = 3
first_n = dict(zip(list(d.keys())[:n], list(d.values())[:n]))
first_n
>>> {'a': 5, 'b': 1, 'c': 8}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us