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)
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:
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 use a variable inside a regular expression?
(12 answers)
Closed 2 years ago.
i have a list :
cpt=0
list=["dermato","bioderma", "gatoderma"]
for l in list:
if re.findall(".*derma.*",l):
cpt=cpt+1
So, instead of putting directly 'derma' as regex , i want :
a= "derma" #initialize
list=["dermato","bioderma", "gatoderma"]
for l in list:
if re.findall(".*a.*",l):
cpt=cpt+1
You can use an f-string:
a= "derma"
list = ["dermato","bioderma", "gatoderma"]
for l in list:
if re.findall(f".*{a}.*", l):
cpt += 1
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:
How do I count the occurrences of a list item?
(29 answers)
Closed 3 years ago.
How do you get the number of times the number 1 appears in the following list:
list = [1,2,3,4,5,1,1]
I have tried the following and it works, but I want to do it without creating an empty list and the if statement, but what I really want is a count of the number of times 1 appears instead of a list with only 1s and then count them.
list = [1,2,3,4,5,1,1]
list_1s = []
for i in list:
if i == 1:
list_1s.append(i)
else:
pass
n_1s = len(list_1s)
print(n_1s)
lst = [1,2,3,4,5,1,1]
Using list comprehensions to solve it -
count_1s = len([i for i in lst if i==1])
print(count_1s)
3