Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print out all rows that have a column equal to a certain value specified by the user?

I am trying to create some code where a user will type in a category value and if a specific column in a row is == to that category then it will display the row in the format below. For some reason, my code only finds the first row in my array that is equal to the column but skips over the rest of the rows even if the column is equal to the users input.

For example, if someone types in Development as the category.. It should all rows with Development set in column 7(ie. rows 1 and 5)
Any help to figure out the issue is much appreciated

contact_list = [[1,'Athena','Brizell','Vitz','9 Northview Hill',5888510700,344-241-3276,'Development','4/27/2021','10/29/2021','Athena Brizell'],
[2,'Isadore','Ander','Wordtune','72660 Orin Road',4848283832,877-155-7495,'Office_fitting','4/25/2021','9/26/2021','Isadore Ander'],
[3,'Hannis','Matches','Realpoint','847 Schurz Park',9052187780,368-121-1215,'Office_fitting','1/8/2021','9/4/2021','Hannis Matches'],
[4,'Cindee','Breadon','Gigashots','3 Bultman Way',4543988898,155-423-5293,'Support','4/25/2021','6/29/2021','Cindee Breadon'],
[5,'Baron','Arnao','Thoughtstorm','856 Blackbird Road',9831465576,207-486-1010,'Development','4/30/2021','10/28/2021','Baron Arnao'],
[6,'Laureen','Kearney','Dablist','70525 Texas Terrace',6091413184,602-843-9500,'Office_fitting','3/3/2021','9/3/2021','Laureen Kearney']]

category = input('Please enter the category you wish to display contacts for (Development/Office_fitting/Support: ')

for row in contact_list:
    if row[7] == category:
        firstname = f'Firstname: {row[1]}\n'
        lastname = f'Lastname: {row[2]}\n'
        company = f'Company: {row[3]}\n'
        address = f'Address: {row[4]}\n'
        landline = f'Phone: {row[5]}\n'
        mobile = f'Mobile: {row[6]}\n'
        category = f'Category: {row[7]}\n'
        date_created = f'Date Created: {row[8]}\n'
        date_modified = f'Date Updated: {row[9]}\n'
        print(firstname, lastname, company, address, landline, mobile, category, date_created, date_modified)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You changed your category variable value that you defined earlier from user input inside your for loop. To fix it, rename the category variable to something else eg category_row inside the for loop.

contact_list = [[1,'Athena','Brizell','Vitz','9 Northview Hill',5888510700,344-241-3276,'Development','4/27/2021','10/29/2021','Athena Brizell'],
[2,'Isadore','Ander','Wordtune','72660 Orin Road',4848283832,877-155-7495,'Office_fitting','4/25/2021','9/26/2021','Isadore Ander'],
[3,'Hannis','Matches','Realpoint','847 Schurz Park',9052187780,368-121-1215,'Office_fitting','1/8/2021','9/4/2021','Hannis Matches'],
[4,'Cindee','Breadon','Gigashots','3 Bultman Way',4543988898,155-423-5293,'Support','4/25/2021','6/29/2021','Cindee Breadon'],
[5,'Baron','Arnao','Thoughtstorm','856 Blackbird Road',9831465576,207-486-1010,'Development','4/30/2021','10/28/2021','Baron Arnao'],
[6,'Laureen','Kearney','Dablist','70525 Texas Terrace',6091413184,602-843-9500,'Office_fitting','3/3/2021','9/3/2021','Laureen Kearney']]

category = input('Please enter the category you wish to display contacts for (Development/Office_fitting/Support: ')

for row in contact_list:
    if row[7] == category:
        firstname = f'Firstname: {row[1]}\n'
        lastname = f'Lastname: {row[2]}\n'
        company = f'Company: {row[3]}\n'
        address = f'Address: {row[4]}\n'
        landline = f'Phone: {row[5]}\n'
        mobile = f'Mobile: {row[6]}\n'
        category_row = f'Category: {row[7]}\n'
        date_created = f'Date Created: {row[8]}\n'
        date_modified = f'Date Updated: {row[9]}\n'
        print(firstname, lastname, company, address, landline, mobile, category_row, date_created, date_modified)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading