Why is this code not iterating through all dict's elements? [duplicate]











up vote
1
down vote

favorite













This question already has an answer here:




  • Recursive Generators in Python

    1 answer




I have a dict with many values, some of which are sub-dicts which also have sub-dicts and so on.



For every value, I need to build a string concatenating the top level key with a dot. So for example, if my dict is {'a': 1, 'b': {'c': 2, 'd': 3, 'e': {'f': 4}}}, I need to build the following strings:



a
b.c
b.d
b.e.f


To do so, I wrote the following code



from types import GeneratorType


def get_parameter(d, top_level_param):
for k, v in d.items():
parameter = top_level_param + '.' + k
if isinstance(v, dict):
get_parameter(v, parameter)
else:
yield parameter


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

for k, v in d.items():
if isinstance(v, dict):
parameter = get_parameter(v, k)
else:
parameter = k

if isinstance(parameter, GeneratorType):
for p in parameter:
print(p)
else:
print(parameter)


However, the output is missing b.e.f and I don't know why. On paper, it looks like it should have been returned by get_parameter().










share|improve this question













marked as duplicate by Thierry Lathuille, Aran-Fey python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 10:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 3




    Inside get_parameter, you call get_parameter(v, parameter), when you probably want to use yield from get_parameter(v, parameter) instead.
    – L3viathan
    Nov 8 at 10:45












  • @L3viathan ohh you are a life saver. That did the trick
    – yzT
    Nov 8 at 10:47

















up vote
1
down vote

favorite













This question already has an answer here:




  • Recursive Generators in Python

    1 answer




I have a dict with many values, some of which are sub-dicts which also have sub-dicts and so on.



For every value, I need to build a string concatenating the top level key with a dot. So for example, if my dict is {'a': 1, 'b': {'c': 2, 'd': 3, 'e': {'f': 4}}}, I need to build the following strings:



a
b.c
b.d
b.e.f


To do so, I wrote the following code



from types import GeneratorType


def get_parameter(d, top_level_param):
for k, v in d.items():
parameter = top_level_param + '.' + k
if isinstance(v, dict):
get_parameter(v, parameter)
else:
yield parameter


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

for k, v in d.items():
if isinstance(v, dict):
parameter = get_parameter(v, k)
else:
parameter = k

if isinstance(parameter, GeneratorType):
for p in parameter:
print(p)
else:
print(parameter)


However, the output is missing b.e.f and I don't know why. On paper, it looks like it should have been returned by get_parameter().










share|improve this question













marked as duplicate by Thierry Lathuille, Aran-Fey python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 10:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 3




    Inside get_parameter, you call get_parameter(v, parameter), when you probably want to use yield from get_parameter(v, parameter) instead.
    – L3viathan
    Nov 8 at 10:45












  • @L3viathan ohh you are a life saver. That did the trick
    – yzT
    Nov 8 at 10:47















up vote
1
down vote

favorite









up vote
1
down vote

favorite












This question already has an answer here:




  • Recursive Generators in Python

    1 answer




I have a dict with many values, some of which are sub-dicts which also have sub-dicts and so on.



For every value, I need to build a string concatenating the top level key with a dot. So for example, if my dict is {'a': 1, 'b': {'c': 2, 'd': 3, 'e': {'f': 4}}}, I need to build the following strings:



a
b.c
b.d
b.e.f


To do so, I wrote the following code



from types import GeneratorType


def get_parameter(d, top_level_param):
for k, v in d.items():
parameter = top_level_param + '.' + k
if isinstance(v, dict):
get_parameter(v, parameter)
else:
yield parameter


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

for k, v in d.items():
if isinstance(v, dict):
parameter = get_parameter(v, k)
else:
parameter = k

if isinstance(parameter, GeneratorType):
for p in parameter:
print(p)
else:
print(parameter)


However, the output is missing b.e.f and I don't know why. On paper, it looks like it should have been returned by get_parameter().










share|improve this question














This question already has an answer here:




  • Recursive Generators in Python

    1 answer




I have a dict with many values, some of which are sub-dicts which also have sub-dicts and so on.



For every value, I need to build a string concatenating the top level key with a dot. So for example, if my dict is {'a': 1, 'b': {'c': 2, 'd': 3, 'e': {'f': 4}}}, I need to build the following strings:



a
b.c
b.d
b.e.f


To do so, I wrote the following code



from types import GeneratorType


def get_parameter(d, top_level_param):
for k, v in d.items():
parameter = top_level_param + '.' + k
if isinstance(v, dict):
get_parameter(v, parameter)
else:
yield parameter


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

for k, v in d.items():
if isinstance(v, dict):
parameter = get_parameter(v, k)
else:
parameter = k

if isinstance(parameter, GeneratorType):
for p in parameter:
print(p)
else:
print(parameter)


However, the output is missing b.e.f and I don't know why. On paper, it looks like it should have been returned by get_parameter().





This question already has an answer here:




  • Recursive Generators in Python

    1 answer








python python-3.x






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 10:42









yzT

722620




722620




marked as duplicate by Thierry Lathuille, Aran-Fey python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 10:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Thierry Lathuille, Aran-Fey python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 10:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 3




    Inside get_parameter, you call get_parameter(v, parameter), when you probably want to use yield from get_parameter(v, parameter) instead.
    – L3viathan
    Nov 8 at 10:45












  • @L3viathan ohh you are a life saver. That did the trick
    – yzT
    Nov 8 at 10:47
















  • 3




    Inside get_parameter, you call get_parameter(v, parameter), when you probably want to use yield from get_parameter(v, parameter) instead.
    – L3viathan
    Nov 8 at 10:45












  • @L3viathan ohh you are a life saver. That did the trick
    – yzT
    Nov 8 at 10:47










3




3




Inside get_parameter, you call get_parameter(v, parameter), when you probably want to use yield from get_parameter(v, parameter) instead.
– L3viathan
Nov 8 at 10:45






Inside get_parameter, you call get_parameter(v, parameter), when you probably want to use yield from get_parameter(v, parameter) instead.
– L3viathan
Nov 8 at 10:45














@L3viathan ohh you are a life saver. That did the trick
– yzT
Nov 8 at 10:47






@L3viathan ohh you are a life saver. That did the trick
– yzT
Nov 8 at 10:47














1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










get_parameter recursively calls itself, but doesn't do anything with the results. Instead, you want to yield those results, too, which you can do with the yield from statement:



def get_parameter(d, top_level_param):
for k, v in d.items():
parameter = top_level_param + '.' + k
if isinstance(v, dict):
yield from get_parameter(v, parameter)
else:
yield parameter





share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote



    accepted










    get_parameter recursively calls itself, but doesn't do anything with the results. Instead, you want to yield those results, too, which you can do with the yield from statement:



    def get_parameter(d, top_level_param):
    for k, v in d.items():
    parameter = top_level_param + '.' + k
    if isinstance(v, dict):
    yield from get_parameter(v, parameter)
    else:
    yield parameter





    share|improve this answer

























      up vote
      4
      down vote



      accepted










      get_parameter recursively calls itself, but doesn't do anything with the results. Instead, you want to yield those results, too, which you can do with the yield from statement:



      def get_parameter(d, top_level_param):
      for k, v in d.items():
      parameter = top_level_param + '.' + k
      if isinstance(v, dict):
      yield from get_parameter(v, parameter)
      else:
      yield parameter





      share|improve this answer























        up vote
        4
        down vote



        accepted







        up vote
        4
        down vote



        accepted






        get_parameter recursively calls itself, but doesn't do anything with the results. Instead, you want to yield those results, too, which you can do with the yield from statement:



        def get_parameter(d, top_level_param):
        for k, v in d.items():
        parameter = top_level_param + '.' + k
        if isinstance(v, dict):
        yield from get_parameter(v, parameter)
        else:
        yield parameter





        share|improve this answer












        get_parameter recursively calls itself, but doesn't do anything with the results. Instead, you want to yield those results, too, which you can do with the yield from statement:



        def get_parameter(d, top_level_param):
        for k, v in d.items():
        parameter = top_level_param + '.' + k
        if isinstance(v, dict):
        yield from get_parameter(v, parameter)
        else:
        yield parameter






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 10:47









        L3viathan

        14.7k12647




        14.7k12647















            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff