0

I am working on many tables, the number of rows of these tables are not the same. Using a for-loop, I could go through the content of the these tables and put it into two lists. For each iteration of the for-loop, the content of these lists will be changed based on the content of these tables. After that, I am looking to put the content of these lists for each iteration into one list.

For example:

I can go manually to put the contents of two list below into a dictionary, but this should be ok, if I have two lists with known dimensions (the number of rows are known), but if I have two lists with unknown number of rows, this wasn't work.

Table: 1

player1 = ['1','2','3']

player2 = ['4','5','6']


d ={}

d['players'] = {}

d['players']['player1'] = []

d['players']['player1'].append(T1[0])

d['players']['player1'].append(T2[0])

d['players']['player2'] = []

d['players']['player2'].append(T1[1])

d['players']['player2'].append(T2[1])

d['players']['player3'] = []

d['players']['player3'].append(T1[2])

d['players']['player3'].append(T2[2])

print(d)

### {'players': {'player1': ['1', '4'], 'player2': ['2', '5'], 'player3': ['3', '6']}}

if I have other two lists from other table where the number of rows are unknown and want to add to the other dictionary where the values (elements) of the dictionary will changes for each iteration of the for-loop, what can I do?

For example:

Table: 2

player1 = ['7','8','9',.....]

player2 = ['1','4','7',.....]

The dictionary should be:

d = {'players': {'player1': ['7', '1'], 'player2': ['8', '4'], 'player3': ['9', '7'], .......}}

At the end, I want to add these two dictionary above to a list

List = [{'players': {'player1': ['1', '4'], 'player2': ['2', '5'], 'player3': ['3', '6']}}, {'players': {'player1': ['7', '1'], 'player2': ['8', '4'], 'player3': ['9', '7'], .......}}]
Pygirl
  • 12,969
  • 5
  • 30
  • 43
Elwakdy
  • 41
  • 1
  • 1
  • 6

3 Answers3

2

You can do this using a for loop like in the following code:

d = {'players':{}}

for i in range(len(player1)): # Assuming length if player1 is same as player2
   player = 'player'+str(i+1) #String Concat 
   d['players'][player] = [player1[i],player2[I]]

To store them in a list, you can replace d using d1 for the first table and d2 for the second table as shown below

final_list = [d1,d2]
Todd Burus
  • 963
  • 1
  • 6
  • 20
shen
  • 25
  • 5
0

You can get your desired result with a dict comprehension by zipping the lists and enumerating:

player1 = ['1','2','3']
player2 = ['4','5','6']

d ={}
d['players'] = {'player'+str(i+1): list(tup) for i, tup in enumerate(zip(player1, player2))}

d will be:

{'players': {
  'player1': ['1', '4'],
  'player2': ['2', '5'],
  'player3': ['3', '6']}
}

Instead of using numerically-based keys in a dict, you might consider just using a list:

player1 = ['1','2','3']
player2 = ['4','5','6']


d ={}
d['players'] = [list(tup) for i, tup in enumerate(zip(player1, player2))]

# access with index instead of player_index
d['players'][0]
# ['1', '4']
Mark
  • 90,562
  • 7
  • 108
  • 148
0

I think I got a very easy answer to your question. Simply try this code... It should be what you are looking for.

player1 = ["7", "8", "9"]

player2 = ["1", "4", "7"]

d = {"players": [{str(player1), str(player2)}]}

d2 = {"players": [{str(player1), str(player2)}]}

dList = []

for player in d:
    dList.append(d)
    dList.append(d2)
Whoozy
  • 161
  • 17