-1

I want to combine two (or more) lists in a single list where each entry in the new list groups items from the original lists. I want to go from this

enter image description here

to this

enter image description here

The First list is string with line breaks as delimiters. And other list is int with line breaks as delimiters. Here is the code

with SampClient(address='rp.valrisegaming.com', port=7777) as client:
        print("Connected Clients")
        print("Name                       | Score".format(client=client))
        print("==================================")
        players = [client.name for client in client.get_server_clients_detailed()]
        sp = '\n'
        sp = sp.join(players)
        score = [client.score for client in client.get_server_clients_detailed()]
        ss =('\n'.join(map(str, score)))
        print(f"{sp}:{ss}")
srm
  • 3,062
  • 16
  • 30

3 Answers3

0

You can run through one of the list and append its items to the other list. for example :-

list_1 = [1,2,3]
list_2 = [4,5,6]

for item in list_1:
    list_2.append(item)

POINT ONE
  • 33
  • 1
  • 7
0

Try this:

players =["a", "b", "c"]
score = ["1", "2", "3"]

res = ["["+ players[i] + "] : " + score[i] for i in range(len(players))]

for e in res:
    print(e)

Gives output

[a] : 1
[b] : 2
[c] : 3
kabooya
  • 447
  • 3
  • 14
0

You can do this using a dictionary, which saves data as key-value pairs. This is perfect for your situation, where you need a name (key) and their score (value). An example at merging your name and value to a dictionary can be like so:

# Initialize dictionary
merged_data = {}

# For each index, for each player
for i in range(len(players)):
    # Add the player with that index, and their score to the dictionary
    merged_data[players[i]] = score[i]

print(merged_data)
Xiddoc
  • 3,369
  • 3
  • 11
  • 37