1

I'd like to be able to get a range fields of a list.

Consider this:

list = ['this', 'that', 'more']

print(list[0-1])

where the [0-1] should return the first and second fields.

kiri
  • 2,522
  • 4
  • 26
  • 44

2 Answers2

5

You will want to use Python's slice notation for this:

>>> lst = ['this', 'that', 'more']
>>> print(lst[:2])
['this', 'that']
>>>

The format for slice notation is [start:stop:step].

Also, I changed the name of the list to lst. It is considered a bad practice to name a variable list since doing so overshadows the built-in.

Community
  • 1
  • 1
3

use:

list = ['this', 'that', 'more']
print(list[0:2])
RMcG
  • 1,045
  • 7
  • 14