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
#1
Hi,
I am trying to find every other value in a dictionary. This is my thought process:

01) Create a loop to go through the dictionary's index.
02) Use a if statement to run a test with % to see if the result is even or odd
03) If the result is not even then I will append this value to a new list.

Here is the code (just trying to see it print before I start appending

odd = ()
for a in (letlist.index):
    if ((a % 2) != 0):
        print (a)
Returned: for a in (letlist.index):
TypeError: 'builtin_function_or_method' object is not iterable

I then tried this
odd = ()
for a in (letlist):
    if ((a % 2) != 0):
        print (a)
Returned: if ((a % 2) != 0):
TypeError: not all arguments converted during string formatting

So then I tried this (trying to make it simpler)
odd = ()
for a in (letlist):
    a = letlist.index
    if (a % 2):
        print (a)
That returned: if (a % 2):
TypeError: unsupported operand type(s) for %: 'builtin_function_or_method' and 'int'

Not sure what i am doing wrong. Is the logic that I wrote in how to solve the problem right? If so what is wrong with my implementation of it?

Thanks.
Reply


Messages In This Thread
Looping through a dictionary for every other value - by fad3r - Jan-25-2018, 02:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Looping to Create Nested Dictionary gngu2691 10 33,718 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