TransWikia.com

Appending an item from one list to another

Stack Overflow Asked by RetroActive on December 8, 2020

I have code here which reads from a file and is supposed to create and print a list of districts, along with a number of how many there are.

def main():

    #open the file
    myFile = open("Data.txt")

    #read the first line
    firstLine = myFile.readline()

    #initialize a counter
    count = 0

    #for each line in the file
    for dataLine in myFile:

        #strip the end of the line
        dataLine = dataLine.rstrip("n")

        #split the line into a list
        dataList = dataLine.split(",")

        #create a new list
        districts = []

        #if dataList[3] is not in districts already
        if dataList[3] in districts == False:
            #append dataList[3] to the districts list
            districts.append(dataList[3])
            count = count + 1

    #print the districts list as well as how many were found
    print("Here is a list of all districts:")
    print(" ")
    print(districts)
    print("There are",count,"districts.")
            
    #close the file
    myFile.close

main()

However, I’m encountering an issue where nothing ever seems to be added to the districts list from dataList. I’m sure it has to do with how I’ve worded my code but I’m unclear on what it could be. Any help would be greatly appreciated.

3 Answers

if dataList[3] in districts always returns false.

Consider changing it to

if dataList[3] not in districts:

Furthermore, as stated in the comments, you might also want to change the line to:

dataLine = dataLine.rstrip()

Correct answer by Gavin Wong on December 8, 2020

To find unique values on a list of data, you can use set

So, here since the data in column 4 is district and you want to find the unique values of this list dataList (which is list of all districts)

unique_districts = list(set(districts)

here set() converts districts to unique values and list() converts it back to a list

def main()
    # get contents of the file into a single list
    with open("Data.txt","r") as fid:
        contents = fid.readlines()

    # assuming the district data is in 4th column
    districts=list(set([line.split(",")[3].rstrip() for line in contents]))
    # length of the districts list is the count

    print("Here are all the districts")
    print(districts)
    print("There are", len(districts), "districts.")
    

Answered by sudhish on December 8, 2020

The command

    #if dataList[3] is not in districts already
    if dataList[3] in districts == False:

should be

    #if dataList[3] is not in districts already
    if dataList[3] not in districts:

Moreover, you have the command

    #create a new list
    districts = []

in your for loop, so you make it empty again and again, giving it no chance to obtain more than one element. Move it out of your loop.

BTW, why don't use the built-in module csv instead of manually parsing every line to create a list of its parts? Here is a modified version of your code:

import csv

def main():

    #open the file
    with open("Data.txt") as my_file:
        reader = csv.reader(my_file, delimiter=",")
        
        #ignore the first line
        next(reader)

        #initialize a counter
        count = 0

        #create a new list
        districts = []
        
        #for each line in the file, AUTOMATICALLY CHANGED BY csv.reader() TO A LIST
        for data_list in reader:

            #if data_list[3] is not in districts already
            if data_list[3] not in districts:
                #append data_list[3] to the districts list
                districts.append(data_list[3])
                count += 1

    #print the districts list as well as how many were found
    print("Here is a list of all districts:")
    print()
    print(districts)
    print("There are", count, "districts.")

Note:

Instead of the name dataList I used the name data_list to be in concordance with PEP 8

Answered by MarianD on December 8, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP