-2

I am much familiar with python and now I started learning how to write codes as short and efficient as possible.

So, this is a piece of code that my teacher told me it is better not to have repeated blocks in my code.

He told me to use dictionaries instead of if statements but I couldn't figure out how to do it because there're for loops.

if day == '1':
    for elem in today:
        if city.lower() in elem or city.upper() in elem:
            print(today1[0])
            print(elem)
            cur_execute("INSERT INTO data VALUES(?, ?)", today1[0], elem)
            break

    else:
        print('data was not found')

if day == '2':
    for elem in tomorrow:
        if city.lower() in elem or city.upper() in elem:
            print(tomorrow1[0])
            print(elem)
            cur_execute("INSERT INTO data VALUES(?, ?)", tomorrow1[0], elem)
            break

    else:
        print('data was not found')

if day == '3':
    for elem in aftertomorrow:
        if city.lower() in elem or city.upper() in elem:
            print(aftertomorrow1[0])
            print(elem)
            cur_execute("INSERT INTO data VALUES(?, ?)", aftertomorrow1[0], elem)
            break

    else:
        print('data was not found')
Filip Młynarski
  • 3,534
  • 1
  • 10
  • 22
LordHomie
  • 23
  • 5
  • 3
    Did your teacher explained what the functions are? – SMA Dec 13 '19 at 13:01
  • You can find the answer here https://stackoverflow.com/questions/38987/how-do-i-merge-two-dictionaries-in-a-single-expression?rq=1 – Victor Lam Dec 13 '19 at 13:01
  • 1
    Your teacher is probably correct, but they are likely to be referring to `today`, `tomorrow`, `aftertomorrow` – roganjosh Dec 13 '19 at 13:02
  • You'd better use switch case https://jaxenter.com/implement-switch-case-statement-python-138315.html – Arlien Dec 13 '19 at 13:02
  • 4
    @Arlien no they wouldn't. That's terrible advice – roganjosh Dec 13 '19 at 13:03
  • @roganjosh Why is that ? Of course in this case he can do it just once with a dict but in general, switch case is way better than 1000 if else.... – Arlien Dec 13 '19 at 13:04
  • @Arlien because the processing is the same for each item in the dict, so they need only iterate the dict and call the function for each value. You don't end up with 1000 `if`/`else`, it _replaces_ them and with a single function. – roganjosh Dec 13 '19 at 13:06
  • Yes ok I know that's why I said "in this case". The first time I just didn't made the link^^ – Arlien Dec 13 '19 at 13:07

2 Answers2

3

you could try:

the_day = { 
    '1': (today, today1),
    '2': (tomorrow, tomorrow1),
    '3': (aftertomorrow, aftertomorrow1)}

selected_day, selected_day1 = the_day[day]
for elem in selected_day:
    if city.lower() in elem or city.upper() in elem:
        print(selected_day1[0])
        print(elem)
        cur_execute("INSERT INTO data VALUES(?, ?)", selected_day1[0], elem)
        break
kederrac
  • 16,819
  • 6
  • 32
  • 55
  • Thank you for you help! but i think there is a mistake here because I have 2 parameters that look the same but actually they are not. today and today1, tomorrow and tomorrow1 , aftertomorrow and aftertomorrow1. so, how to deal with that? – LordHomie Dec 13 '19 at 13:39
  • @LordHomie check now? – kederrac Dec 13 '19 at 13:42
  • You can replace `if city.lower() in elem or city.upper() in elem` with `if city.lower() in elem.lower()` – bruno desthuilliers Dec 13 '19 at 14:12
2

You have two options replace the if by a dictionary or replace only the values that are different in a for loop

Dictionary instead of if-conditions

if x == 1:
    function1()
if x == 2:
    function2()
if x == 3:
    function3()
# ...

can be converted to:

functions = {1: function1, 2: function2, 3: function3}
functions[x]()

This is possible because functions are also objects and can be stored in a dictionary as value.

Replace values by dict

You can also change your code, this way so that you only have one loop: e.g.

days = {'1': today, '2': tomorrow, '3': aftertomorrow}
for elem in days[day]:
    if city.lower() in elem or city.upper() in elem:
        print(days[day][0])
        print(elem)
        cur_execute("INSERT INTO data VALUES(?, ?)", days[day][0], elem)
        break
else:
    print('data was not found')
Uli Sotschok
  • 1,206
  • 1
  • 9
  • 19