0

Consider I have three lists, stored in file sports.py, which is separate from the .py file I'm working in:

games_1 = ["Tennis", …]
games_2 = ["Cricket", …] 
games_3 = ["Golf", …] 

I also have a variable var_game == "Football".

I would like to check which lists var_game appears in. So far I have tried:

for N in range(1,3):
    if var_game in sports.games_N:
        gamesN_check = 1
    else:
        gamesN_check = 0

However, Python does not let me use N to cycle through the lists in this way. How do I need to use it with the correct syntax?

Many thanks in advance!

1
  • 5
    Use a dictionary Commented Aug 23, 2018 at 19:46

3 Answers 3

3

I would recommend packaging your lists in a dictionary and evaluating in the manner below:

games_1 = ['Tennis', 'Football', 'Basketball']
games_2 = ['Cricket', 'Football', 'Lacrosse'] 
games_3 = ['Golf','Baseball', 'Rugby', 'Track'] 

games_N = {'games_1': games_1, 'games_2': games_2, 'games_3': games_3}

var_game = 'Football'

games_N_check = {key: 1 if var_game in val else 0 for key, val in games_N.items()}

Outputs:

{'games_1': 1, 'games_2': 1, 'games_3': 0}

Additionally

A quick means to create your dictionary from a series of lists, so that you do not have to explicitly name every list:

games_lists = [['Tennis', 'Football', 'Basketball'],['Cricket', 'Football', 'Lacrosse'],['Golf','Baseball', 'Rugby', 'Track']]

games_N = {'games_{}'.format(idx+1): game_list for idx, game_list in enumerate(games_lists)}

Gives:

{'games_1': ['Tennis', 'Football', 'Basketball'], 'games_2': ['Cricket', 'Football', 'Lacrosse'], 'games_3': ['Golf', 'Baseball', 'Rugby', 'Track']}
Sign up to request clarification or add additional context in comments.

1 Comment

This worked excellently for me, and allows the scaling up to more than 3 lists that I will need later. I wasn't aware of the dictionary function in Python (yes, I'm really that new to it). Thank you!
0

Instead of using 3 variables for games, Why don't you make a list of list of games

games = [["Tennis", …], ["Cricket", …], ["Golf", …]]

and a list of results

games_check = []

And then check for every game in games, if var_games in game, and if so - add 0 to the list of results games_check, otherwise add 1

for game in games:
    if var_game in game:
        games_check.append(1)
    else:
        games_check.append(0)

Or to make it more 'Pythonic'

games_check = [(1 if var_game in game else 0) for game in games]

Comments

0

I think for your situation, you can just do this:

if var_game in sports.games_1:
    games1_check = 1
elif var_game in sports.games_2:
    games2_check = 1
elif var_game in sports.games_3:
    games3_check = 1 

1 Comment

What's the use of the for loop then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.