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]
3
  • You can use range(1, 5) Commented Sep 9, 2019 at 17:27
  • I'm pretty sure your current syntax allows you to select rows 1 to 3 from the dataframe as well. See pandas.pydata.org/pandas-docs/stable/reference/api/… Commented Sep 9, 2019 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. Commented Sep 9, 2019 at 17:39

5 Answers 5

1

You can use

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

list() converts given range object into list

Sign up to request clarification or add additional context in comments.

3 Comments

There is no range object, because your code produces a syntax error.
Yeah i agree, there should be , instead of :
Now you may check this please.
0

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

Comments

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]

1 Comment

Thank you. I'm used to python 2 range. What if I want a list of numbers and not range object?
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))

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.