How to find the middle element/index of a list in Python?
10,633
Solution 1
In case you want to round down for odd amount of list elements, you can use math.trunc
for all cases:
l = [1,3,7,"this",3,"that",7]
middle = math.trunc(len(l)/2)
print(middle)
>>> 3
Solution 2
using a Floor division works fine too -- the digits after the decimal point are removed ^^
listt = [1,3,7,"this",3,"that", 7]
# use // Floor Division
middle = len(listt)//2
print(middle)
Related videos on Youtube
Comments
-
ToErotimatiko almost 2 years
I want to be able to print the middle element of a list . The list might be just strings, integers or mixed. I tried this, by finding the index of the middle element, but doesn't work. (It prints 2)
list = [1,3,7,"this",3,"that",7] if int(len(list))<2: middle = 1 // if it has less than 2 entries, the first entry is the middle one. elif int(len(list)) %2 == 0 : middle = int(len(list)/2) else: middle = int((len(list)/2) - 1) print(list[middle])
-
accdias over 4 yearsFix your code snippet because that is not valid Python syntax. Also note that
list
is a reserved word and should not be used as variable names. -
urban over 4 yearsAlso it seems to me that
int((len(list)/2) - 1)
should belen(list)/2
since in python 2 this would be5/2 == 2
. In python3 this should belen(list)//2
(note double//
for int division), which also gives you 2 -
ToErotimatiko over 4 yearsOh ~ I fixed the list , I can't believe i did that kind of mistake.
-
philipxy over 4 yearsRe the 3NF question you just deleted: You say "I don't see how this is any different [from] the original form that is given." But "this" is 4 schemas/relations & "the original form" is 1. (Are you confusing FDs with schemas?) PS There is a typo in your R schema--it contains an FD. PS Re better formatting for your R schema: Please before you post look at the formatted version of your post below the edit box. Read the edit help re inline & block formats for code & quotations. Also re line breaks.
-
ToErotimatiko over 4 yearsThank you very much for your answer. I will, from now on. Posting from mobile ( this is where I first made the question) is exhausting and not helpful for formatting. I also didn't like my algorithm source , because it was in a Greek book that is not available online. I will make sure to translate the steps.
-
-
accdias over 4 yearsYour answer is not right for all cases. What happens if the number of elements is even or if the list has just one element or none?
-
accdias over 4 yearsBest answer with no doubt. No ifs, no elses, just math.
-
Shinobi San over 4 yearsI think that it depends on how is the code going to be used. This is the code that i would write if someone asks me to write a code to get the middle element of an array.
-
accdias over 4 yearsIf you look at the code on the OP (even though it is not completely right) you can see that when the list is even, the middle is the last element on the first half of the list. Your solution gives the first element on the second half.