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)
Share:
10,633

Related videos on Youtube

ToErotimatiko
Author by

ToErotimatiko

Trying to self-teach Java.Very early Beginner.

Updated on June 04, 2022

Comments

  • ToErotimatiko
    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
      accdias over 4 years
      Fix 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
      urban over 4 years
      Also it seems to me that int((len(list)/2) - 1) should be len(list)/2 since in python 2 this would be 5/2 == 2. In python3 this should be len(list)//2 (note double // for int division), which also gives you 2
    • ToErotimatiko
      ToErotimatiko over 4 years
      Oh ~ I fixed the list , I can't believe i did that kind of mistake.
    • philipxy
      philipxy over 4 years
      Re 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
      ToErotimatiko over 4 years
      Thank 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
    accdias over 4 years
    Your 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
    accdias over 4 years
    Best answer with no doubt. No ifs, no elses, just math.
  • Shinobi San
    Shinobi San over 4 years
    I 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
    accdias over 4 years
    If 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.