I need some advises how i can improve my code and make it more pythonic. Cause i feel like my code is too long.

This is my solution to cs50 DNA problem:

import sys
import csv

if len(sys.argv) != 3:
    print(f"Usage: python {sys.argv[0]} database.csv sequence.txt")

database = sys.argv[1]
sequence = sys.argv[2]
people = {}
shtares = []    # sh(ort)ta(ndem)re(peats), because str in low case is ocupied
keys = []

# Saves all STRs to "shtares",
with open(database, "r") as file:
    reader = csv.reader(file)
    for row in reader:
        shtares = row[1:]
    # Saves all data in dictionary "people" and all keys in "keys"
    for row in reader:
        people[row[0]] = [int(x) for x in row[1:]]

with open(sequence, "r") as file:
    dna = file.read()

src = []    # Samples repeat count(src)
x = 0
# Save number of consecutively repeated STRs for each STR
for shtare in shtares:
    i = 0
    max_count = 0
    cur_count = 0
    shtare_length = len(shtare)
    # Check if STR matches curent DNA part
    while i <= len(dna) - shtare_length:
        dna_part = dna[i:i + shtare_length]
        if cur_count != 0:
            if dna_part != shtare:
                cur_count = 0
        if dna_part == shtare:
            cur_count += 1
            i += shtare_length
            i += 1
        if cur_count > max_count:
            max_count = cur_count
    x += 1

# Looking for DNA sample STRs count matches to someone's in people STRS count
for x in range(len(keys)):
    for y in range(len(shtares)):
        if src == people[keys[x]]:

print("No match")

Program takes as input one sequence of DNA in .txt file - for example:


and one table in .csv file - for example:

Alice 2 8 3
Bob 4 1 5
Charlie 3 2 5

And also i feel like using csv.DictReader must be usefull i can't find any information how to save data from .csv file to dictionary. I thought this will do:

import sys
import csv

if len(sys.argv) != 3:
    print(f"Usage: python {sys.argv[0]} database.csv sequence.txt")

database = sys.argv[1]
sequence = sys.argv[2]
people = {}

with open(database, "r") as file:
    reader = csv.DictReader(file)
    for row in reader:
        people[row[0]] = row[1:]

Bot id doesn't. I found out that slicing doesn't work in dictionary. So the other question is - How to properly save data from .csv file to dictionary?

1 Answer 1


This isn't that long. Maybe consider using a dictionary for shtares as well. You don't really make good use of dictionaries here. You have several of these for x in range(len(keys)) type loops, which I'm generally not a fan of. With a little bit of restructuring, and dictionaries over lists, these could be replaced with for key in keys: which I think looks better, and is easier to understand.

  • Thanks, now i see that i don't even need keys list. And i also want to ask is there an easier way to properly save data like in this case to dictionary? I use csv.reader for it, how to do it with csv.DictReader?
    – TomOrrow
    Feb 14 at 16:22
  • @TomOrrow my preferred way of doing this is with DictReader. Then if you iterate over each dictionary you get from your DictReader object, that dictionary represents a person to match against the dna sequence. The fieldnames are repeated in each dictionary. Feb 15 at 10:25
  • But i still can't understand how to save from .csv file to dictionary by using DictReader. I updates my question with way i thought it should work but it doesn't. And my other solution using DictReader looks worse than the first one.
    – TomOrrow
    Feb 15 at 11:58
  • @TomOrrow maybe we should continue this elsewhere. Do you have a discord account? The cs50 server is pretty good for stuff like this: discord.gg/cs50 My username there is the same as here. Feb 15 at 12:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .