Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Iterate dict in python using next()

Is it possible to iterate dictionary in python using next(). (I need to access keys and values). Also would it work for extendable dictionaries with non-fixed length?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Use items to get the pairs key/value and iter() to be able to call next

content = dict(zip(range(10), range(10, 20)))
print(content)  # {0: 10, 1: 11, 2: 12, 3: 13, 4: 14, 5: 15, 6: 16, 7: 17, 8: 18, 9: 19}

iterable = iter(content.items())
print(next(iterable))  # (0, 10)
print(next(iterable))  # (1, 11)
print(next(iterable))  # (2, 12)
print(next(iterable))  # (3, 13)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading