Dictionary keys via for in Python

When iterating over a dictionary with the for loop, its keys are always printed by default.

Example

Let us have a dictionary dct:

pythondct = {
  'a': 1,
  'b': 2,
  'c': 3
}

Let's run it in a loop:

pythonfor key in dct:
  print(key)

As a result of executing the code, all keys will be displayed:

python'a'
'b'
'c'

Example

You can also get the dictionary keys using the keys method. The method returns a special object that can be iterated over in a loop:

pythonfor key in dct.keys():
  print(key)

Result of code execution:

python'a'
'b'
'c'

Practical tasks

Задание 1

Given a dictionary:

pythontst = {
  'a': 1,
  'b': 2,
  'c': 3,
  'd': 4,
  'e': 5
}

Output its keys to the console:

python'a'
'b'
'c'
'd'
'e'
Задание 2

Given a dictionary:

pythontst = {
  2: 'a',
  4: 'b',
  6: 'c',
  8: 'd'
}

Print its keys to the console, except 8.

Задание 3

Given a dictionary:

pythontst = {
  '1': 'a',
  '2': 'b',
  '3': 'c',
  '4': 'd'
}

Obtain the following tuple from its keys:

python('2', '3', '4')