Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7598897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:31:33+00:00 2026-05-30T22:31:33+00:00

Question: Given a list of integers, write python code to create a new list

  • 0

Question: Given a list of integers, write python code to create a new list with the same number of elements as the original list such that each integer in the new list is the sum of its neighbors and itself in the original list. Example: is listA = [10,20,30,40,50], listB = [30,60,90,120,90]

I’m struggling with finding the right way to set up this code. I know how to define the function but how do you set up the adding of neighbor integers/itself? Any help would be appreciated.

What I have so far:

def sumNeighbors(list, start = 0, end =None):
    if end is None:
       end = len(list)

sum = 0
i = start
while i < end:
    sum += list[i]
    i += 1
return sum

text = raw_input ("Enter an integer (period to end): ")
list = []
while text != '.':
    textInt = int(text)
    list.append(textInt)
    text = raw_input("Enter an integer (period to end): ")

print "List: ", list
print "sum: ", sumNeighbors(list)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:31:34+00:00Added an answer on May 30, 2026 at 10:31 pm

    I just special cased the ends of the list. Basically, at the start of the list, you only want add the right neighbor, and at the end of the list, you only want to add the left neighbor.

    So, I start by appending the first item which is just the value at index 0 + the value at index 1.

    new_list.append(list[0] + list[1])

    Then, I use a while loop to iterate through all the middle indexes.

    For each of those, we are summing list[x - 1] + list[x] + list[x + 1]

    Careful to start summing the middle with index 1, not 0, as 0 was already special cased. Then make sure you don’t go off the end, by using a for loop that goes to len(list) - 1. For the last item, you just sum list[x - 1] + list[x]

    Here’s the whole code:

    def sum_neighbors(list):
        new_list = []
        new_list.append(list[0] + list[1])
        x = 1
        while x < len(list) - 1:
            new_list.append(list[x - 1] + list[x] + list[x + 1])
            x += 1
        new_list.append(list[x - 1] + list[x])
    
        return new_list
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two versions of the same question : 1-Given a list of number
This is a similar question to this one here . Given a list of
I've been given the question: Define a predicate ordered/1, which checks if a list
Question: Given a SQL string like CREATE VIEW TestView AS SELECT value1, value2 FROM
QUESTION: Given a cell index (red) compute the array index (black) that surround the
That's the question xD Given an instance of a CCSprite in cocos2d in iphone,
This is probably a really silly question but, given the example code at the
I was asked this question: You are given two lists of integers, each of
Just been looking at a code golf question about generating a sorted list of
During the qualification round, the following question was asked: You've been given a list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.