This question already has answers here:
How can I generate a list of consecutive numbers? [duplicate]
(8 answers)
Closed 4 years ago.
I'm sure python has a built in way to create an x size list where the contents are 0 through x-1, but I don't know how to do it. I've searched on Google as well as on here, I'm sure that I must've not been using the correct wording to find what I needed. Please help.
Ex: len([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) = 10
You are looking for the range builtin:
range(x) # python 2
list(range(x)) # python 3
Related
This question already has answers here:
Python Dictionary Comprehension [duplicate]
(9 answers)
Closed 4 months ago.
dict = {x: arr.count(x) for x in arr}
want to break it in more lines of code how to do it? or is there any other way to write it to understand the concept clearly?
Use for loop
arr = [1, 2, 1, 3, 4, 5, 10]
dict = {}
for x in arr:
dict[x]=arr.count(x)
print(dict)
This question already has answers here:
How can I randomly select an item from a list?
(17 answers)
Closed 4 months ago.
I want to generate random ints from 4 specific numbers (1,2,5,10).
I only want the output to be one of these. How?
You can use the choice method:
random.choice([1, 2, 5 ,10])
Hope this helps
You can use random.choice like so:
import random
nums = [1, 2, 5, 10]
print(random.choice(nums)) # prints either 1, 2, 5, or 10
You can view more at the documentation: https://docs.python.org/3/library/random.html#random.choice
This question already has answers here:
How do I reverse a list or loop over it backwards?
(37 answers)
Understanding slicing
(38 answers)
Closed 6 months ago.
In C++, we do reverse(nums.begin() + 1, nums.end() - 2) for reversing a list or vector in ranges. So, is there something like this in Python for achieving the same?
All you need to do is use list slicing syntax:
>>> l = [1,2,3,4,5,6,7,8,9,10]
>>> l[3:6] = l[5:2:-1]
>>> l
[1, 2, 3, 6, 5, 4, 7, 8, 9, 10]
This question already has answers here:
Understanding slicing
(38 answers)
Closed 2 years ago.
today I was learning about list slicing in python, and I have a question about the rules by which python "operates/slices" these lists. How is it possible that I don't get an error message when clearly the range is wrong?
li=[1,2,3,4,5,6]
li[1:12:1]
[2,3,4,5,6]
there isnt an index error it just stops at the end:
li=[1,2,3,4,5,6]
print(li[:10])
print(li[:7])
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
This question already has answers here:
How to remove multiple indexes from a list at the same time? [duplicate]
(8 answers)
Deleting multiple elements from a list
(32 answers)
Closed 4 years ago.
I have a list of values for example:
[1,2,3,4,5,6,7,8,9,10]
And specific indexes like:
[0,3,5]
I want something that returns deleting the values which the index belongs to the [0,3,5] array:
[2,3,5,7,8,9,10]
Any ideas for Python 3?
Thanks
Using enumerate and list comprehension.
Ex:
l = [1,2,3,4,5,6,7,8,9,10]
toDelete = [0,3,5]
print([v for i,v in enumerate(l) if i not in toDelete])
Output:
[2, 3, 5, 7, 8, 9, 10]