0

I want to print the first 10 lines of a list

I am new to python. This actually my first python project. I don't know how to go about it.

The list looks like this. How do we call such a list in python?

numbers = [[0.4437678, 0.4318693, 0.3873539, 0.03706345]]
          [[0.4437677, 0.4318692, 0.3873538, 0.03706344]]
          [[0.2260452, 0.2189278, 0.1944221, 0.01853172]]
          [[0.2260452, 0.2189278, 0.1944227, 0.01853173]]
          [[0.2260452, 0.2189278, 0.1944227, 0.01853173]]
          ...
          ...
          ...
          [[0.2260452, 0.2189278, 0.1944221, 0.01853172]]
          up to about 500 lines

I want to read and print the first 10 lines of this list

Kevin
  • 16,549
  • 8
  • 60
  • 74
Samuel
  • 27
  • 2
  • 2
    Please update the question with the code you have tried. – quamrana Sep 18 '19 at 15:45
  • 1
    That seems to be kind of a duplicate of https://stackoverflow.com/questions/10897339/python-fetch-first-10-results-from-a-list – gelonida Sep 18 '19 at 15:48
  • @quamrana I haven't tried anything. I will try whatever suggestions given here – Samuel Sep 19 '19 at 14:06
  • 1
    That's not the way stackoverflow.com works. You should have tried something and when it doesn't work you will have all the pieces you need to ask a question, eg. This is my data, this is my program, I run it and get this output, but was expecting this output instead. – quamrana Sep 19 '19 at 14:18
  • 1
    btw, the data you *do* specify is not python code, so all the helpful people here (I currently see 4 answers) have had to make something up that works for themselves. When you say 'That didn't work for me.' you are playing "fetch me a rock". – quamrana Sep 19 '19 at 14:22

4 Answers4

3

You have a nested list. See below for how to access using indices:

test = [['item1']]

print(test)
>> [['item1']]

print(test[0])
>> ['item1']

print(test[0][0])
>> 'item1'

You haven't given how your series of lists is stored, but you'll probably be able to cut off how many you want using slicing:

series[:10]

So if your series looked like this (a list of nested lists):

series = [[['item1']],
[['item2']],
[['item3']],
[['item4']],
[['item5']],
[['item6']],
[['item7']],
[['item8']],
[['item9']],
[['item10']],
[['item11']]]

And you wanted to grab the first 10, it would be:

series[:10]

If you want to unnest, you would need to access each maybe in a loop:

for i in range(0, 10):
    print(series[i][0][0])

To give you:

item1
item2
item3
item4
item5
item6
item7
item8
item9
item10
MyNameIsCaleb
  • 4,409
  • 1
  • 13
  • 31
  • Thanks but it didn't work for me. The list example you used is comma separated but mine is not. – Samuel Sep 19 '19 at 14:08
  • 1
    For your original data, do a `type(numbers)` and tell me what that says. What you display above is not a real data type in Python so there is something missing that we don't have. – MyNameIsCaleb Sep 19 '19 at 14:27
1

You don't have specify your dataset assign to which variable. If you use this

mydata = [[0.4437678, 0.4318693, 0.3873539, 0.03706345]]
          [[0.4437677, 0.4318692, 0.3873538, 0.03706344]]
          [[0.2260452, 0.2189278, 0.1944221, 0.01853172]]
          [[0.2260452, 0.2189278, 0.1944227, 0.01853173]]
          [[0.2260452, 0.2189278, 0.1944227, 0.01853173]]
          ...
          ...
          ...
          [[0.2260452, 0.2189278, 0.1944221, 0.01853172]]
          up to about 500 lines

use this code

mydata[0:10]

This will give you first 10 raws of your dataset

Kalana
  • 5,631
  • 7
  • 30
  • 51
1

The most efficient way is to use list slicing which goes like input_list[0:10] where 0 is inclusive index and 10 is exclusive. So you would end up processing elements from 0 through 9.

for element in input_list[0:10]:
    # process with element
    ...
Swadhikar
  • 2,152
  • 1
  • 19
  • 32
1

An example with a generator and no leading zero when slicing the list.

my_list = [1,2,3,4,5,6,7,8,9,10,11]

print(* (x for x in my_list[:10]), sep='\n' )

1
2
3
4
5
6
7
8
9
10

Here, I believe * is used to unpack the generator object.

Also, pandas uses the shorthand [:] syntax a lot so it will be good to get used to.

Kermit
  • 4,922
  • 4
  • 42
  • 74