Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a list of 100 integers whose values equal their indexes

Tags:

python

list

Create a list of 100 integers whose value and index are the same, e.g.

mylist[0] = 0, mylist[1] = 1, mylist[2] = 2, ...

Here is my code.

x_list=[]

def list_append(x_list):
    for i in 100:
        x_list.append(i)

        return(list_append())
    print(x_list)
like image 944
Bob Avatar asked Sep 26 '13 03:09

Bob


People also ask

How do I make a list of 100 numbers in Python?

Python Create List from 0 to 100. A special situation arises if you want to create a list from 0 to 100 (included). In this case, you simply use the list(range(0, 101)) function call. As stop argument, you use the number 101 because it's excluded from the final series.

How do you make a list of numbers from 0 to 100 in Python?

Using the range() function to create a list from 1 to 100 in Python. In Python, we can use the range() function to create an iterator sequence between two endpoints. We can use this function to create a list from 1 to 100 in Python. The function accepts three parameters start , stop , and step .

How do you find all the indexes of an element in a list?

One of the most basic ways to get the index positions of all occurrences of an element in a Python list is by using a for loop and the Python enumerate function. The enumerate function is used to iterate over an object and returns both the index and element.

How do you create an integer list in Python?

In Python, a list is created by placing elements inside square brackets [] , separated by commas. A list can have any number of items and they may be of different types (integer, float, string, etc.).

How do you find the index of a value in a list Python?

To facilitate this, Python has an inbuilt function called index(). This function takes in the element as an argument and returns the index. By using this function we are able to find the index of an element in a list in Python.


1 Answers

Since nobody else realised you're using Python 3, I'll point out that you should be doing list(range(100)) to get the wanted behaviour.

like image 129
Veedrac Avatar answered Oct 02 '22 04:10

Veedrac