Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting data to put in a variable from a list sequence

I'm trying to find a way to store in a variable of all the numbers from the players within allPlayers

I have been recommended to use the for loop which works but when you go to use it globally it only shows the last number not all of them. Preferably I would like it to be all within one line putting it into a variable but whatever is the simplest.

player1 = ['David.G', '1204', '4th Catogory']
player2 = ['John.D', '1000', 'Unranked']
player3 = ['Barry.M', '1932', '1st Catogory']
player4 = ['Steven.H', '1844', '1st Catogory']
allPlayers = [player1, player2, player3, player4]

for player in allPlayers:
    fideRankings = player[1]
    global fideRankings

print("")
print(fideRankings)

Result is: 1204 1000 1932 1844

1844

I was looking for it to print the same four numbers outside the for loop.

like image 972
PlainText Avatar asked May 31 '26 16:05

PlainText


1 Answers

player1 = ['David.G','1204','4th Catogory']
player2 = ['John.D','1000','Unranked']
player3 = ['Barry.M','1932','1st Catogory']
player4 = ['Steven.H','1844','1st Catogory']
allPlayers = [player1,player2,player3,player4]

for i in allPlayers:
    print(i[1],end=" ")

if you just want to print it outside of for loop:

player1 = ['David.G','1204','4th Catogory']
player2 = ['John.D','1000','Unranked']
player3 = ['Barry.M','1932','1st Catogory']
player4 = ['Steven.H','1844','1st Catogory']
allPlayers = [player1,player2,player3,player4]

string = ""
for i in allPlayers:
    string = string + " " + str(i[1])

print(string)

you can always use string.split(" ") to get those numbers in list if you need to use those numbers in future.

like image 96
Umesh Jadhav Avatar answered Jun 02 '26 04:06

Umesh Jadhav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!