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
#3
It is for dictionary, This is the challenge i was given.

Here is the complete code. I am now running into issues with the original dictionary being out of scope and then new problems when I try to use global.

Complete code as it stands
mport string
letlist = list(string.ascii_lowercase)


#Create string of letters
letstring = string.ascii_lowercase

#Create list of numbers
list1=[]
for i in range (1, 27):
    list1.append(i)

#Create dictionary for part 1
def dictfunc():
    global zipped
    zipped = dict(zip(list1,letlist))

dictfunc()


def test ():
    for a in zipped:
        return a[::2]
    print (a)
Errors:
File "C:/Users/c0unt3rpl4y/PycharmProjects/House/scratchpad.py", line 26, in <module>
test()
File "C:/Users/c0unt3rpl4y/PycharmProjects/House/scratchpad.py", line 23, in test
return a[::2]
TypeError: 'int' object is not subscriptable

I am really new to this so I keep googling various ideas and then taking snippets of code and throwing them in and trying to make them work.

I will try it your way now and see if that works better

Your code snipped

for num in zipped:
    if num % 2:
        print(num)
Does indeed return the odds numbers but it does not return every other value of the dictionary which is the final piece of it. I think I need to maybe use index?
Reply


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

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