0

I am trying to print the first list of a list of lists:

Here's my attempt:

  1. I defined a function that splits a list into sub-lists inside a list.
  2. Defined a initial list (codes)
  3. Tried to print just the first list in codes_chunks, by using a for-loop structure.
########## define a split function ########

def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]

########## splitting the list #############

countries = ['Spain', 'Portugal']
codes=['A1', 'A2', 'A3', 'A4', 'A6', 'A7']

codes_chunks = list(chunks(codes, 2))
###########################################

# tried to print just for the first list #

for country in countries:
     for chunks_sample in codes_chunks[0]:
         for code in chunks_sample:
               print(code+'-'+country)
###########################################

Console Output:

# A-Spain
# 1-Spain
# A-Spain
# 2-Spain
# A-Portugal
# 1-Portugal
# A-Portugal
# 2-Portugal

Expected Output:

# A1-Spain
# A2-Spain
# A1-Portugal
# A2-Portugal

What am I missing?

Thanks in advance.

3 Answers 3

1
for chunks_sample in codes_chunks[0]:

This line. Shouldn't it be code_chunks and not code_chunks[0] ? or codes_chunks[0:2]

My output with that change:

A1-Spain
A2-Spain
A3-Spain
A4-Spain
A6-Spain
A7-Spain
A1-Portugal
A2-Portugal
A3-Portugal
A4-Portugal
A6-Portugal
A7-Portugal

with codes_chunks[0:2]

A1-Spain
A2-Spain
A3-Spain
A4-Spain
A1-Portugal
A2-Portugal
A3-Portugal
A4-Portugal

codes_chunks[0:1]

A1-Spain
A2-Spain
A1-Portugal
A2-Portugal
Sign up to request clarification or add additional context in comments.

Comments

1

If you print codes_chunks:

print(codes_chunks) # [['A1', 'A2'], ['A3', 'A4'], ['A6', 'A7']]

Not you're selecting code_cunks[0] which is ['A1','A2'].

If you now iterate over this you'll get A1 which is required but you're iterating again which results in A & 1. So, you need to remove the extra loop.

Correct code for desired output:

########## define a split function ########

def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]

########## splitting the list #############


countries = ['Spain', 'Portugal']
codes = ['A1', 'A2', 'A3', 'A4', 'A6', 'A7']

codes_chunks = list(chunks(codes, 2))
###########################################

# tried to print just for the first list #

for country in countries:
    for chunks_sample in codes_chunks[0]:
        print(chunks_sample+'-'+country)
###########################################

Comments

0

TL;DR:

Looping over codes_chunks[0] already provides the "code" A1 and A2, thus your innermost loop is just one layer too deep.

Step by step:

>>> codes_chunks = list(chunks(codes, 2))
[['A1', 'A2'], ['A3', 'A4'], ['A6', 'A7']]

Thus, codes_chunks[0] in your second-order loop is only ['A1', 'A2'].

>>> for country in countries:
...     # code_chunks[0] = ['A1', 'A2']
...     for chunks_sample in codes_chunks[0]:
...         # you don't need another loop here
...         # chunks_sample = 'A1' or 'A2'
...         # `for code in chunks_sample` would loop over 'A1' yielding 'A' and '1'
...         print(chunks_sample+'-'+country)
A1-Spain
A2-Spain
A1-Portugal
A2-Portugal

Generalization to more chunks

If you need to generalize your loops to all codes_chunks, you have two options:

Option 1: Wrap another for-loop around everything else, to alternate the countries in the output:

>>> for codes_chunk in codes_chunks:
...    for country in countries:
...        for chunks_sample in codes_chunk:
...            print(chunks_sample+'-'+country)
A1-Spain
A2-Spain
A1-Portugal
A2-Portugal
A3-Spain
A4-Spain
A3-Portugal
A4-Portugal
A6-Spain
A7-Spain
A6-Portugal
A7-Portugal

Option 2: Add the same loop inside your outermost loop to complete countries one-by-one:

>>> for country in countries:
...    for codes_chunk in codes_chunks:
...        for chunks_sample in codes_chunk:
...            print(chunks_sample+'-'+country)
A1-Spain
A2-Spain
A3-Spain
A4-Spain
A6-Spain
A7-Spain
A1-Portugal
A2-Portugal
A3-Portugal
A4-Portugal
A6-Portugal
A7-Portugal

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.