This question already has answers here:
Identify groups of continuous numbers from consecutive list in python
(4 answers)
Closed 2 years ago.
The challenge is to find the first non-consecutive number in python. I have managed to do it with a library called more_itertools, but the challenge requires that no library be involved. How can I solve it?
This is my code:
from more_itertools import consecutive_groups
def first_non_consecutive(l):
g = []
for grp in consecutive_groups(l):
g.append(list(grp))
if len(g) > 1:
return g[1][0]
Use enumerate with start parameter to your advantage here.
def first_non_consecutive(lst):
for i, j in enumerate(lst, lst[0]):
if i!=j:
return j
first_consecutive([1,2,3,4,6,7,8])
# 6
Related
This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 10 months ago.
I am learning python from Youtube and want to write a python code which removes duplicates from the list. This is my code :
nums = [1,1,2,2,2,2,3,3,4,8,5,6,6,4,4,7,5,8]
j = 1
for i in nums :
if nums.count(i) > 1 :
while j < nums.count(i) :
nums.remove(i)
j += 1
print(nums)
I know there are other ways to do it but i want to know why this one isn't working. are the all the modules correct?
You are not resetting your counter (j) each time and it continues to increment. Try moving the j = 1 into the for loop.
But you shouldn't ever mutate a list as you iterate through it.
This question already has answers here:
Python Sets vs Lists
(10 answers)
Complexity of *in* operator in Python
(3 answers)
Closed 1 year ago.
I was solving a hackerrank challenge.(in Python 3)
At one point I needed to sort a list and work with it.
(2 <= len(array) <= 10^5)
The list is containing unique integers.
I used arr.sort(), but I was getting "Time limit exceeded" message
Then I found if I use set(arr), it runs fast and passes all test cases.
here's is my function for more clarification
def pairs(k, ar):
ar.sort()
c=0
for i in ar:
if i+k in ar:
c+=1
return c
This gets me "Time limit exceeded"
def pairs(k, ar):
ar=set(ar)
c=0
for i in ar:
if i+k in ar:
c+=1
return c
This one with set() runs faster.
Why is this set() is faster than sort().
Don't they use same sorting algorithm?
This question already has answers here:
How to change for-loop iterator variable in the loop in Python?
(6 answers)
Updating Index Value in For Loop
(2 answers)
Closed 1 year ago.
I have a similar code that needs incrementing and while loop cant be used there,
m = range(10)
for i in range(len(m)):
print(i)
i+=2
Do it like this:
m = range(10)
nb = 0
for i in range(len(m)):
print(nb)
nb+=2
The problem is that you wanted to use i for two different tasks.
This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
How to remove list elements in a for loop in Python? [duplicate]
(6 answers)
Closed 3 years ago.
def thirdMax(nums):
highest = max(nums)
for i in nums:
if i == highest:
nums.remove(i)
print(nums)
secondHighest = max(nums)
for i in nums:
if i == secondHighest:
nums.remove(i)
thirdHighest = max(nums)
return thirdHighest
thirdMax([1,2,3,3,3,3,3,3,4,4,4,4,4,4,4])
My code is supposed to return the third highest distinct number. My code isn't seeming to work accordingly.
Sort the list (to make sure). Convert to a set. Convert back to a list and take the 3rd from the right.
l = [1,2,3,3,3,3,3,3,4,4,4,4,4,4,4]
l.sort()
s = set(l)
l = list(s)
third = l[-3]
You can combine some of these steps, but I wrote them all out for clarity :)
This question already has answers here:
Sum a list of numbers in Python
(26 answers)
Closed 4 years ago.
I'm starting to learn how to code and I've been trying to work out how to sum numbers from a list.
list1 = [1,2,3,4,5]
Using a for-loop, how would I set a variable to the sum of the list (15)?
Using a for loop:
acc = 0
for num in list1:
acc += num
Another approach:
acc = sum(list1)