0

In r code, the sequence of integers from 1 to 4:

1:4

How can I replicate this in python 3? The use case is selecting from a pandas dataframe using iloc:

df.iloc[1:4]
Machavity
  • 30,841
  • 27
  • 92
  • 100
DDDD
  • 31
  • 5
  • You can use `range(1, 5)` – lmiguelvargasf Sep 09 '19 at 17:27
  • I'm pretty sure your current syntax allows you to select rows 1 to 3 from the dataframe as well. See https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html – absolutelydevastated Sep 09 '19 at 17:29
  • Note that `df.iloc[1:4]` is essentially just syntactic sugar for `df.iloc[slice(1,4)]`; it's unrelated to the generation of an actual sequence of integers. It's up to the particular implementation of `__getitem__` that receives that `slice` object to decide how exactly to treat it. – chepner Sep 09 '19 at 17:39

5 Answers5

1

You can use

mylist=list(range(1,5))
print(mylist)

list() converts given range object into list

Hetal Thaker
  • 717
  • 5
  • 12
0

In Python, in order to generate a range of numbers use the function range.

range(1, 5)

This will generate numbers from 1 to 4. If you are in Python 2, range will give you a list, and if you are in Python 3, it will give you a range object.

If you are in Python 3, and you want a list, just use the list function to convert your range to a list

>>> list(range(1, 5)
[1, 2, 3, 4]
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
  • Thank you. I'm used to python 2 range. What if I want a list of numbers and not range object? – DDDD Sep 09 '19 at 17:29
0

Use range(1, 5) for giving you that set of integers from 1 to 4 (inclusive of both)

Matthew Gaiser
  • 4,558
  • 1
  • 18
  • 35
0

To generate a range of number from 1 to 4 (inclusive), use: range(1, 5) or range(1, 4+1)

Just a few things to note:

  • The code range(x, y) generates a range from (and including) x up to (but not including) y. If you want to include y, add 1 to it (as I did above).

  • range(x, y) works a bit differently in Python 2 vs. Python 3. I won't go into the details here, but I recommend using Python 3 (which hopefully you're already using).

  • range(x) is will generate a range from (and including) zero up to (but not including) x. So range(x) is basically the same as range(0, x).

  • The range function will generate a range object; that is, it doesn't actually create a list of all the integers between x and y. If you do want a list of integers between x and y, use this code:

    some_list = list(range(x, y))
    

However, if you're looking to pull out a slice of values out of a sequential container, this might work:

from itertools import islice
values = islice(df.iloc, 1, 5)  # returns an iterator
values = list(values)  # extracts the values from the iterator into a list
J-L
  • 1,786
  • 10
  • 13
0

Python's slicing syntax doesn't produce a range of integers; it produces one or more slice objects which are passed to the __getitem__ method of the appropriate type. That is,

df.iloc[1:4]

is equivalent to either

def.iloc[slice(1,4)]

or

df.iloc.__getitem__(slice(1,4))
chepner
  • 497,756
  • 71
  • 530
  • 681