Hi guys,

I'm created the variables in the loops to generating the list of values and channels, I want to use the variable outside of the loop so I can get the list of values. I have a problem with get the list of values.

When I try this:

# set the channels text
for index in range(0, CHANNELS_PER_PAGE):
    channel = channelList[index]
    channel_index = index

    for channels_id in range(4127, 4548, 70):
       ID = []
       if channel is not None:
          ID = channels_id
    print ID
    print channel
    self.getControl(ID).setLabel(channel)

I will get the output like this:

01:59:10 T:6768  NOTICE: 4547
01:59:10 T:6768  NOTICE: 4547
01:59:10 T:6768  NOTICE: 4547
01:59:10 T:6768  NOTICE: 4547
01:59:10 T:6768  NOTICE: 4547
01:59:10 T:6768  NOTICE: 4547
01:59:10 T:6768  NOTICE: 4547

It should be like this:

01:59:10 T:6768  NOTICE: 4127
01:59:10 T:6768  NOTICE: 4197
01:59:10 T:6768  NOTICE: 4267
01:59:10 T:6768  NOTICE: 4337
01:59:10 T:6768  NOTICE: 4407
01:59:10 T:6768  NOTICE: 4477
01:59:10 T:6768  NOTICE: 4547

The problem are lie in this code:

for channels_id in range(4127, 4548, 70):
    ID = []
    if channel is not None:
       ID = channels_id
print ID

I'm stored the variable ID under the loop called for channels_id to generating the list of values. I want to use that variable outside of the loop so I can use the getControl method to get the list of values while I can use the other variable channel to get the list of channels.

Can you please show me how I can use the variable ID outside of the loop where I can use with the other variable channel so I can use it on getControl method?

Dani AI

Generated

Quick summary and fixes (ties to the replies from and ):

The observed output happens because the ID collection is being recreated or mis-used inside the inner loop, and sometimes you end up passing a single-element list to getControl (which prints like [4547]) instead of an integer. Generate the sequence of control IDs once, keep it outside the per-channel loop, and pair each channel with one integer ID.

A minimal, clear pattern to pair channels and control IDs (use a precomputed list and zip so indexes line up):

control_ids = list(range(4127, 4548, 70))   # compute once

for channel, cid in zip(channelList[:CHANNELS_PER_PAGE], control_ids):
    if channel is None:
        continue
    self.getControl(cid).setLabel(channel)

For the “program IDs” sequence where you want blocks of 70 but skip the final number in each block (so 4128..4196 then skip 4197, then 4198..4266 then skip 4267, etc.), a generator keeps memory use low and makes the rule explicit:

def gen_program_ids(start=4128, stop=4616, block=70):
    cur = start
    while cur < stop:
        end_of_block = min(cur + block - 1, stop)
        for i in range(cur, end_of_block):
            yield i
        cur += block

for pid in gen_program_ids():
    # use pid (always an int)
    print(pid)

Debugging tips and cautions:

  • Check types with print(type(var)) if output shows brackets — you likely have a list instead of an int.
  • If channelList and control_ids are different lengths, zip silently truncates; use explicit checks or enumerate + index checks when that matters.
  • Keep accumulator lists or ID sequences declared outside loops (as implied) and avoid reassigning them inside inner loops.
  • The generator approach above matches ’s intent but makes the “skip the last in each block” rule explicit and easy to reuse.

Recommended Answers

All 5 Replies

You create a new list on every pass through the for() loop, so everything except for the final value in the range was garbage collected. Move the ID=[] somewhere outside of the for loop(s) so it contains all values.

for channels_id in range(4127, 4548, 70):
   ID = [] # re-created every time

Also, take a look at this tutorial on lists and how to append or extend items to them. Click Here

Are you sure that your code is correct?

Because it will show me like this:

19:29:55 T:2336  NOTICE: [4547]
19:29:55 T:2336  NOTICE: [4547]
19:29:55 T:2336  NOTICE: [4547]
19:29:55 T:2336  NOTICE: [4547]
19:29:55 T:2336  NOTICE: [4547]
19:29:55 T:2336  NOTICE: [4547]
19:29:55 T:2336  NOTICE: [4547]

When I use this:

# set the channels text
for index in xrange(0, CHANNELS_PER_PAGE):
     channel = channelList[index] # Get channel
    channel_index = index


    for channels_id in range(4127, 4548, 70):
        ID = []
        if channel is not None:
           ID.append(channels_id)
    print ID

Are you sure you even read the post as the list is still declared under the same for() loop.

yes I did thank you very much for your help.

This is the last thing i need. I want to generate the program id under the #generate the programs id

Using this code:

for row in programs:
    program = row[1].encode('ascii'), str(row[2]), str(row[3])
    title = row[1].encode('ascii')
    program_start_date = str(row[2])
    program_end_date = str(row[3])

    if program_width < 1:
        program_title = ''
    else:
        program_title = title

        #generate the programs id

I want to generate the ID that start with 4128 to count each number until it get reaction to 4196 then skip the next number and start it with 4198 then count the number until it get reaction to 4266 then skip the next number and continue to count the numbers.

Something similar like this:

for programs_id in range(4128, 4616, 70):
    for programs_start_end in range(69):
        _ID = programs_id + programs_start_end
        programs_button = self.getControl(ID)
        print _ID

I can't use the variable outside of the loop. So i want to find a different way without use the loop.

Do you know how I can generate the ID without the loop?

You can create a list of numbers

>>> L = range(4128, 4616)
>>> L = [x for x in L if (x-4128) % 70 != 69]
>>> L
[4128, 4129, 4130, 4131, 4132, 4133, 4134, 4135, 4136, 4137, 4138, 4139, 4140, 4141, 4142, 4143, 4144, 4145, 4146, 4147, 4148, 4149, 4150, 4151, 4152, 4153, 4154, 4155, 4156, 4157, 4158, 4159, 4160, 4161, 4162, 4163, 4164, 4165, 4166, 4167, 4168, 4169, 4170, 4171, 4172, 4173, 4174, 4175, 4176, 4177, 4178, 4179, 4180, 4181, 4182, 4183, 4184, 4185, 4186, 4187, 4188, 4189, 4190, 4191, 4192, 4193, 4194, 4195, 4196, 4198, 4199, 4200, 4201, 4202, 4203, 4204, 4205, 4206, 4207, 4208, 4209, 4210, 4211, 4212, 4213, 4214, 4215, 4216, 4217, 4218, 4219, 4220, 4221, 4222, 4223, 4224, 4225, 4226, 4227, 4228, 4229, 4230, 4231, 4232, 4233, 4234, 4235, 4236, 4237, 4238, 4239, 4240, 4241, 4242, 4243, 4244, 4245, 4246, 4247, 4248, 4249, 4250, 4251, 4252, 4253, 4254, 4255, 4256, 4257, 4258, 4259, 4260, 4261, 4262, 4263, 4264, 4265, 4266, 4268, 4269,...]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.