0
for x in range(10):
    data = [['Day','Date','Week'], ['Day','Date','Week'], ['Day','Date','Week']]
    print data[:1]

Question: Is it possible to display only first list element in for loop?

2
  • Of which list? :) The answer will vary. Commented Nov 13, 2013 at 11:40
  • @StoryTeller Sorry, I mean first list in data lists -> ['Day','Date','Week'] Commented Nov 13, 2013 at 11:43

1 Answer 1

4

To display only the first element, you can use index 0

data = [['Day','Date','Week'], ['Day','Date','Week'], ['Day','Date','Week']]
print data[0]

Output

['Day','Date','Week']

You can display that in for loop like this

for x in range(10):
    print data[0]

Output

['Day', 'Date', 'Week']
['Day', 'Date', 'Week']
['Day', 'Date', 'Week']
['Day', 'Date', 'Week']
['Day', 'Date', 'Week']
['Day', 'Date', 'Week']
['Day', 'Date', 'Week']
['Day', 'Date', 'Week']
['Day', 'Date', 'Week']
['Day', 'Date', 'Week']

To get all the first elements in the inner lists

data = [['Day','Date','Week'], ['Day','Date','Week'], ['Day','Date','Week']]
print [item[0] for item in data]

Output

['Day', 'Day', 'Day']
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks. It's helpful. What if I need first list from my data lists?
Can I in print data[0] get only first element from my 10 elements?
Thank you very much. Ok. This return Day from ['Day', 'Date', 'Week']. I mean all this first list ['Day', 'Date', 'Week'] from my 10 lists
@netman97 I didnt understand. Could you please show the 10 lists?
In that case, you dont need for loop at all. Remove the for loop and simply say print data[0]
|

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.