Python Shorts | How to Get Dictionary Keys and Values at the Same Time in a for Loop in Python

Veröffentlicht am: 26 Mai 2021
auf dem Kanal: choobtorials
771
24

In this video, we'll learn how to simultaneously get the keys and the values when looping through a dictionary using a for loop.



==============================



When looping through a dictionary using a for loop, each key and its corresponding value can be retrieved at the same time with the help of the items() method.

Here in the for loop’s header, call the items method on the dictionary like so, and then there should be 2 variables separated by a comma. In each iteration, the key is assigned to the first one and the value is assigned to the second one.

pets = {'bob': 'the turtle', 'longy': 'the dachshund'}

for k, v in pets.items():
print(k, v)

Let’s run the code, and here we see the expected output.



==============================

Twitter:   / choobtorials  


#PythonShorts #PythonTips


Auf dieser Seite können Sie das Online-Video Python Shorts | How to Get Dictionary Keys and Values at the Same Time in a for Loop in Python mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer choobtorials 26 Mai 2021 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 771 Mal angesehen und es wurde von 24 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!