Find the biggest value in a given list [duplicate] - python

This question already has answers here:
How to find the maximum number in a list using a loop?
(11 answers)
Closed 2 years ago.
by using only a temporary variable, a for loop, and an if to compare the values-hackinscience.org
Find the biggest value in a given list.
the_list = [
143266561,
1738152473,
312377936,
1027708881,
1495785517,
1858250798,
1693786723,
1871655963,
374455497,
430158267,
]

max_in = 0
for val in the_list:
if val > max_in:
max_in = val
There is the for, there is the if, max_in is somehow a temp var cause it changes over the loop. You get it.

No need for either of that. Use max(the_list).

Related

How to find position of big value in python list array [duplicate]

This question already has answers here:
Pythonic way to find maximum value and its index in a list?
(11 answers)
Getting the index of the returned max or min item using max()/min() on a list
(23 answers)
Closed 1 year ago.
list = [3,5,1,8,9]
I want to find positions of the maximum value in the list
This is pretty simple, but it will give you the index of the first occurrence:
>>> l = [3,5,1,8,9]
>>> l.index(max(l))
4
I strongly suggest you not use the name of built-in functions as list for variables.

How could we access the value in a dictionary that is present as a list? [duplicate]

This question already has answers here:
Finding the average of a list
(25 answers)
Find a value from a dictionary in python by taking a key from user input [closed]
(3 answers)
Closed 2 years ago.
marks={'A':[50,70,90],'B':[60,80,70],'C':[70,80,90]}
In the above dictionary, I need to access the value of B and to find the average of list (i:e:(60+80+70)/3). How could I access the value and find the average? What I tried was...
marks={'A':[50,70,90],'B':[60,80,70],'C':[70,80,90]}
get_name=input()
for i in marks:
if i==get_name:
for j in i:
add += marks[j]
print(add/3)
It shows up error. How to access the values in the dictionary of the list[60,80,70] with respect to key 'B'.
Here's a one liner -
avg = sum(marks['B'])/3
sum() will total the value in that respective list and you just have to divide it by the size of the list.
input = 'A'
average = sum(marks[input])/len(marks[input])
marks={'A':[50,70,90],'B':[60,80,70],'C':[70,80,90]}
For the above code, marks[get_name] should print out the list [60,80,70] through which the mean can be then taken.
To iterate over a dictionary you would use, for key, val in marks.items() and check if the provided user input equals to one of the key and then take the average.

How do I make different variables in for loops In Python [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 3 years ago.
I am making a program that has a for loop and every time the loop is ran I want it to make a new variable like
for item in range(0, size)
(Make new variable bit1, bit2, bit3, bit4, etc with value of 0)
Is this possible?
Create a List of variables and append to it like this:
bits = []
for item in range(0, size)
bits.append(0)
# now you have bits[0], bits[1], bits[2], etc, all set to 0
We can do this by appending to the vars() dictionary in the program.
for index, item in enumerate(range(size)):
vars()[f'bit{index+1}'] = 0
Try calling the names now:
>>> bit1
0
And while this works, I'd recommend using a list or a dict instead.

Delete a set of positions from a Python list [duplicate]

This question already has answers here:
How to remove multiple indexes from a list at the same time? [duplicate]
(8 answers)
Closed 5 years ago.
Although this should be rather easily done with a for loop, I'm wondering wether there is a concise way to delete a set of positions from a Python list. E.g.:
l = ["A","B","C","D","E","F","G"]
pos = [4,6]
# Is there something close to
l.remove(pos)
Best
del is what you are looking for if you have consecutive indices:
From the documentation:
There is a way to remove an item from a list given its index instead of its value: the del statement
Otherwise, you could generate a new list, by filtering out indices, which you aren't interested in
result = [x for i, x in enumerate(l) if i not in pos]

how to define a list with predefined length in Python [duplicate]

This question already has answers here:
Create a list with initial capacity in Python
(11 answers)
Closed 9 years ago.
I want to do the following :
l = list()
l[2] = 'two'
As expected, that does not work. It returns an out of range exception.
Is there any way to, let say, define a list with a length ?
Try this one
values = [None]*1000
In place of 1000 use your desired number.

Categories