Python Forum
Looping through a dictionary for every other value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping through a dictionary for every other value
#4
Quote:it does not return every other value of the dictionary
Every other value of a dictionary doesn't really make sense.
Also, consider the global keyword off limits for the next 5 years or so; seriously.
It should not be used for anything like this.

Please provide the exact text of the challenge you are trying to solve.

>>> from string import ascii_lowercase as lower
>>> my_dict = dict(zip(lower, range(1,27)))
>>> my_dict
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'g': 7, 'f': 6, 'i': 9, 'h': 8, 'k': 11, 'j': 10, 'm': 13, 'l': 12, 'o': 15, 'n': 14, 'q': 17, 'p': 16, 's': 19, 'r': 18, 'u': 21, 't': 20, 'w': 23, 'v': 22, 'y': 25, 'x': 24, 'z': 26}
>>> odd_entries = {k:v for k,v in my_dict.items() if v % 2}
>>> odd_entries
{'a': 1, 'c': 3, 'e': 5, 'g': 7, 'i': 9, 'k': 11, 'm': 13, 'o': 15, 'q': 17, 's': 19, 'u': 21, 'w': 23, 'y': 25}
>>>
Reply


Messages In This Thread
RE: Looping through a dictionary for every other value - by Mekire - Jan-25-2018, 03:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Looping to Create Nested Dictionary gngu2691 10 33,643 Jun-22-2018, 04:11 PM
Last Post: anickone

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020