Python Forum
How to find difference between elements in a list? Using beginner Basics only.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to find difference between elements in a list? Using beginner Basics only.
#1
Hello,

I have been going crazy on a portion of an assignment, I am hoping to get help with. Only using Basic begginer python coding...Functions, loops, files, lists, modules.

In my assignment I have been given a .txt file with a several number on each line ( the numbers represent population amounts). I need to read this file into a list(list 1, I can do this.) Then I have to make 2 more list, one of a list of years between 1950 and 1990(list 2, I can do this) and then a list which stores the population changes from one year to the next(list 3, Need help with this! where to start? For / in?).

I need help on how to make the the list 3, the difference between the populations. How do you compute the difference between sequential elements in a list?
Here is what I have so far... I wanted to make sure I could compute each list needed first before getting into placing these lists into functions and def them ect.


popfile = open("somenumbers.txt", "r") #open File and read into list population
population = popfile.readlines() #list1 from file
popfile.close()
print()

#make list 2 of years 1950-1991
year = list(range(1950,1991))
print(year)

#List 3 Difference in populations
diff_list=[]
for x in range(1,len(population)):
print(population)
diff_list.append(population[x]-population[x-1])
print("the diff_list",diff_list)

With the above I am getting unsupported operand - for str and str:

The file contains 41 lines with each line having a number which represents a population between 1950-1991. (eventually I will use the list of years I make, so someone can enter a year and have the population returned along with other info. When the .txt file is viewed it is in the following format for 41 total lines.(example numbers)

124499
193284
138479
193849
540392
293485
129384
Reply


Messages In This Thread
How to find difference between elements in a list? Using beginner Basics only. - by Anklebiter - Nov-11-2020, 08:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  help with adding duplicates elements together in a list 2ECC3O 5 2,190 Sep-10-2022, 07:11 AM
Last Post: 2ECC3O
  Unexpected behavior accessing list elements. tonyflute 2 2,370 Apr-09-2021, 02:36 PM
Last Post: tonyflute
  Help with argument basics mickandralphscrier 5 2,678 Feb-17-2021, 10:11 PM
Last Post: mickandralphscrier
  Get 5 most unique combinations of elements in a 2D list wanttolearn 1 2,385 Sep-24-2020, 02:26 PM
Last Post: buran
  To find the index of the first occurrence of the key in the provided list Angry_bird89 4 3,465 Jun-20-2020, 06:53 PM
Last Post: Angry_bird89
  Loop through elements of list and include as value in the dictionary Rupini 3 2,760 Jun-13-2020, 05:43 AM
Last Post: buran
  How do you find the length of an element of a list? pav1983 13 5,187 Jun-13-2020, 12:06 AM
Last Post: pav1983
  Compare elements of tuples / Find edges between nodes Daniel94 1 1,853 Apr-06-2020, 03:32 PM
Last Post: deanhystad
  How can I print the number of unique elements in a list? AnOddGirl 5 3,435 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  Find first letter from a list DariusCG 3 2,661 Nov-27-2019, 11:08 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020