2

I am looking for a more pythonic way to loop though a list starting an an index then return the values contained in sub-lists eg:

values = [[1,2], [2], [1,3,4]]
n=1
for i, item in enumerate(values[n:]):
    i += n
    if i < len(values):
        for sub_value in values[i]:
            print("index: "+str(i)+ " list: "+str(item)+ " sub value: "+str(sub_value))

The code works as intended but is pretty ugly, any ideas to simplify it?

7
  • Why are you doing i += n? Commented Mar 21, 2015 at 3:18
  • @thefourtheye I am pulling out the i+nth item from the main list, otherwise i starts at zero and I get the wrong sub-values Commented Mar 21, 2015 at 3:21
  • oop though a list starting an an index then return the values contained in sub-lists This contradicts with your program Commented Mar 21, 2015 at 3:24
  • This code looks a bit... off... is this your desired output?: index: 1 list: [2] sub value: 2 index: 2 list: [1, 3, 4] sub value: 1 index: 2 list: [1, 3, 4] sub value: 3 index: 2 list: [1, 3, 4] sub value: 4 Commented Mar 21, 2015 at 3:24
  • @Hybrid, yes that's is the output I'm after - I just realised may be cleaner to return the sub-lists as they are ie [2] and then [1,3, 4] and then just iterate over them. Commented Mar 21, 2015 at 3:29

2 Answers 2

2

I'm not sure I completely understand what you are trying to achieve.If you want to print a flat list of the items from index 1 you could do this:

[item for sublist in values[1:] for item in sublist]

which produces:

[2, 1, 3, 4]
Sign up to request clarification or add additional context in comments.

Comments

0

I'm going to take a swing here and guess that you are trying to make a simple python function that loops through a list and prints out every element in the sublists. Here is the easiest way to do it:

def get_sublists(start=0):
    values = [[1,2], [2], [1,3,4]] 
    index = start 
    item = 0
    for value in values[index:]: 
        for sub_value in value: 
            print("Item: %s // Index: %s // List: %s // Sub Value: %s" % (item, index, values[index], sub_value)) 
            item += 1
        index += 1

get_sublists(1)

This will print out the following:

Item: 0 // Index: 1 // List: [2] // Sub Value: 2                                                                                                                                                                                                       
Item: 1 // Index: 2 // List: [1, 3, 4] // Sub Value: 1                                                                                                                                                                                                 
Item: 2 // Index: 2 // List: [1, 3, 4] // Sub Value: 3                                                                                                                                                                                                 
Item: 3 // Index: 2 // List: [1, 3, 4] // Sub Value: 4

I am not 100% sure about the question because it is a bit ambigous, so let me know if you have any further modifications.

2 Comments

your comments made me think a bit more about what I was trying to do and I came up with this: def getSubLists(start, values): for i, item in enumerate(values[start:]): for value in item: print(str(value)) values = [[1,2], [2], [1,3,4]] getSubLists(1, values) This way I can start my iteration at a specific location
Oh, I finally understand. In that case, you can use @craighagerman's solution since it uses List Comprehension, which is very efficient. If you want a more detailed output, you can use the function I provided above.

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.