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
#11
(Jan-25-2018, 05:48 PM)j.crater Wrote: The thing is dictfunc() function will not be very helpful because it does not return anything.

Really? Because if I print letlist it returns values. So I am just trying to get letlist into that function. That is initially why I tried using global because I didn't realize you could pass functions to other values.

Here is the output if I add a print for letlist:
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h', 8: 'i', 9: 'j', 10: 'k', 11: 'l', 12: 'm', 13: 'n', 14: 'o', 15: 'p', 16: 'q', 17: 'r', 18: 's', 19: 't', 20: 'u', 21: 'v', 22: 'w', 23: 'x', 24: 'y', 25: 'z'}

import string
def dictfunc():
    letlist = dict(enumerate(string.ascii_lowercase))
    print (letlist)
dictfunc()
Reply
#12
That's right, letlist is computed properly and you can print it. But you need to return it from the function. Otherwise this:
my_dict = dictfunc()
doesn't make much sense. What are you assigning to my_dict?
At least that is what causes the error I get by running your code. What error do you get?
Reply
#13
I did it like that to avoid using the global variable.

Here is the original problem which I solved with a lot of help from Mekire

This seems to not have that issue

# 1) Start with this list:
#['a', 'b', 'c', ..., 'z'}
#
# And create a function that turns it into this dictionary:
#
# {1: 'a', 2: 'b', 3: 'c', ..., 26: 'z'}
#
# 2) Start with this string:
#
# 'abc...z'
#
# And create a function that turns it into this dictionary:
#
# {1: 'a', 2: 'b', 3: 'c', ..., 26: 'z'}
#
# 3) Take one of the above functions and include only the odd numbers / letters:
#
# {1: 'a', 3: 'c', ..., 25: 'y'}
#
# This is hard but you can get it.  You'll have to learn about counters.  Either:
#
# idx += 1 or for idx, item in enumerate(<list goes here>)


# 1) Start with this list:
#['a', 'b', 'c', ..., 'z'}

#Let's impress Doctor Housman. Instead of writing out the lists manually lets do it programatically shall we?
#Create list of letters
import 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():
    zipped =dict(zip(list1,letlist))

dictfunc()

#Create dictionary for part 2
def dictfun2():
    zipped2 = dict(zip(list1, letstring))

dictfun2()

def odds_only():
    my_dict = dictfunc()
    return {k: v for k, v in my_dict.items() if k % 2}


print(odds_only())
Right there I am passing dictfunc to my_dict or are you saying that code is actually not doing anything?

I will look into the return function right now.
Reply
#14
You are on the right track and definitely being wise to follow Mekire's advice, but let me quote him:
Quote:Communication with functions should be done via argument passing and return values. Not global variables.

The code is missing passing arguments to functions and returning values (except for odds_only()).
Reply
#15
Just adding return fixed it. I must learn about this return fucntion!!
Reply
#16
return is a statement that is used in functions. You need to return something from a function if you want to assign that result to another variable, as you've just learned. There is plenty of resources, here is the official Python docs on functions ;)
Reply


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