0

I have a lists of lists in variable lists something like this:

[7, 6, 1, 8, 3]
[1, 7, 2, 4, 2]
[5, 6, 4, 2, 3]
[0, 3, 3, 1, 6]
[3, 5, 2, 14, 3]
[3, 11, 9, 1, 1]
[1, 10, 2, 3, 1]

When I write lists[1] I get vertically:

6
7
6
3
5
11
10

but when I loop it:

for i in list:
    print(i)

I get this horizontally.

7
6
1
8
3
etc...

So, how it works? How can I modify loop to go and give me all vertically?

2
  • 4
    How you say *When I write lists[1] I get vertically:*6 7 6 3 5 11 10? its impossible with the lists that you have provided! Commented Mar 1, 2015 at 21:34
  • 1
    And the for loop wouldn't give that output either. Please give real examples, and clarify what you're asking: What does "give me all vertically" mean exactly? Commented Mar 1, 2015 at 21:36

3 Answers 3

4

Short answer:

for l in lists:
   print l[1]  
Sign up to request clarification or add additional context in comments.

2 Comments

Not sure, because index is out of range
Can you elaborate? I'm using a collection iterator in the outer loop rather than a range. Of course, this assumes that all of your lists have at least two elements.
1

Lists of lists

list_of_lists = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]]
for list in list_of_lists:
    for x in list:
        print x

1 Comment

I was trying that, it gave me rows, but I want columns.
0

Here is how you would print out the list of lists columns.

lists = [[7, 6, 1, 8, 3],
[1, 7, 2, 4, 2],
[5, 6, 4, 2, 3],
[0, 3, 3, 1, 6],
[3, 5, 2, 14, 3],
[3, 11, 9, 1, 1],
[1, 10, 2, 3, 1]]

for i in range(0, len(lists[1])):
    for j in range(0, len(lists)):
        print lists[j][i],
    print "\n"

2 Comments

Yea, so close but I want loop all over the lists and get lists of columns. Not only first column.
@user3136858 can you be a little more specific? Example output? Ill modify my answer then to be exactly what you want

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.