How to resolve this IndexError out of range?
<p>Why am I getting this list index out of range error? And how can I fix it?</p>

<p>I tried entering into after the <em>else</em> in the logic, but the result was the same.         </p>

<pre><code>if int_list_repost is '':
            int_list_repost = [0]
</code></pre>

<p>but the result was the same</p>

<blockquote>
  <p>Traceback (most recent call last):   File
  "C:/Users/ayevtushenko/PycharmProjects/Tuts/Post_Engagement_pin_logic.py",
  line 99, in 
      get_post('<a href="https://twitter.com/ClinRev" rel="nofollow">https://twitter.com/ClinRev</a>')   File "C:/Users/ayevtushenko/PycharmProjects/Tuts/Post_Engagement_pin_logic.py",
  line 95, in get_post
      'retweeted_posts': len(int_list_repost[1:]), 'pinned_retweets': int_list_repost[0], IndexError: list index out of range</p>
</blockquote>

<pre><code>import requests
import re
from bs4 import BeautifulSoup

def get_post(url):

    source_code = requests.get(url)
    plain_text = source_code.text
    my_soup = BeautifulSoup(plain_text)

    mylist = []
    int_list = []
    mylist_repost = []
    int_list_repost = []
    pinned = ""

    #   GETS "PINNED" TEXT IF PINNED
    for content in my_soup.findAll('span', {'class': 'js-pinned-text'}):
        pinned = str(content.string)

    #   PUTS FAVORITE METRICS INTO LIST
    for content in my_soup.findAll('div', {'class': 'ProfileTweet-action ProfileTweet-action--favorite js-toggleState'}):
        fetch = content.contents[1]
        for tag in fetch.findAll('span', {'class': 'ProfileTweet-actionCountForPresentation'}):
            mylist.append(tag.string)
            if str(tag.string).isdigit():
                int_list.append(int(tag.string))

    #   PUTS RE-POST METRICS INTO LIST
    for content in my_soup.findAll('div', {'class': 'ProfileTweet-action ProfileTweet-action--retweet js-toggleState js-toggleRt'}):
        fetch = content.contents[1]
        for tag in fetch.findAll('span', {'class': 'ProfileTweet-actionCountForPresentation'}):
            mylist_repost.append(tag.string)
            if str(tag.string).isdigit():
                int_list_repost.append(int(tag.string))

    like_page_utilization = str((len(int_list)/len(mylist))*100)+'%'
    repost_page_utilization = str((len(int_list_repost)/len(mylist_repost))*100)+'%'

    #   TOTAL ENGAGEMENT METRICS
    largest_list = [len(int_list), len(int_list_repost)]
    largest_list_max = max(largest_list)
    total_engagements_overall = sum(int_list)+sum(int_list_repost)

    overall_engagement_utilization = str((largest_list_max/len(mylist))*100)+'%'

    if pinned != 'Pinned Tweet':
        return {'liked_posts': len(int_list), 'total_likes': sum(int_list),
                'pinned_likes': 0, 'pinned': 'F', 'like_page_utilization': like_page_utilization,
                'repost_page_utilization': repost_page_utilization,
                'overall_engagement_utilization': overall_engagement_utilization,
                'retweeted_posts': len(int_list_repost), 'pinned_retweets': 0,
                'total_retweets': sum(int_list_repost),
                'total_engagements_overall': total_engagements_overall}
    else:
        return {'liked_posts': len(int_list[1:]), 'total_likes': sum(int_list[1:]),
                'pinned_likes': int_list[0], 'pinned': 'T', 'like_page_utilization': like_page_utilization,
                'repost_page_utilization': repost_page_utilization,
                'overall_engagement_utilization': overall_engagement_utilization,
                'retweeted_posts': len(int_list_repost[1:]), 'pinned_retweets': int_list_repost[0],
                'total_retweets': sum(int_list_repost[1:]),
                'total_engagements_overall': total_engagements_overall}
</code></pre>

<p>You've initialized an array, which is an object.  When you ask if the variable is null, that will always be false.  It's not null, it's an object.  </p>

<p>If you want to test to see if your array is empty use this code:</p>

<pre><code>is len(int_list_repost) == 0:
    print("int_list_repost is empty")
</code></pre>

<p>But that's not very pythonic.  The test for empty arrays should be</p>

<pre><code>is not int_list_repost:
    print("int_list_repost is empty")
</code></pre>

<p>The error message you are getting is because <code>int_list_repost[0]</code> will error out if you have an empty array.</p>

python indexing