Ethan Chung
Ethan Chung

Reputation: 25

For loop only printing out last element in Python

I have a problem with the for loop. For some reason when I execute the loop it only prints out 1 element of the for loop. This is the data I have

lucidData = [['Dupable', '246', 'Mercedes', '25200'], ['Qilani', '240', 'Kanna', '21000'], ['MihKyle', '250', 'Mihile', '34750'], ['Goobtella', '245', 'Kanna', '27000'], ['Gosenka', '240', 'Buccaneer', '25762'], ['Eeldu', '244', 'Pathfinder', '33359'], ['YoungHanny', '249', 'Dual Blade', '36000'], ['Yumiikko', '248', 'Kanna', '32721'], ['lRollingDeep', '247', 'Adele', '29201'], ['LordPrime', '247', 'Kaiser', '33000'], ['GoiabaSama', '255', 'Demon Avenger', '964310'], ['GordoChorizo', '246', 'Dual Blade', '30387'], ['ScarletReyne', '251', 'Ark', '29651'], ['StupidGameu', '251', 'Demon Avenger', '31674'], ['TURTLESmrff', '242', 'Pathfinder', '2400'], ['Sleepybearrx', '240', 'IL Mage', '27953'], ['Wremy', '251', 'Hero', '35773'], ['woele', '245', 'Phantom', '33578'], ['Codé002', '240', 'Kanna', '22219'], ['FullSword', '250', 'Adele', '29548'], ['Ch0senAlpha', '242', 'Hero', '28521'], ['MeserMule', '254', 'Kanna', '33974'], ['KanaoSayo', '245', 'Kanna', '23000']]

and when I use this for loop to try and get rid of the quotes and brackets:

def formatting(data):
  for i in range(len(data)):
    datas = ', '.join(data[i])
  return datas

print(formating(lucidData))

I get only:

KanaoSayo, 245, Kanna, 23000

Which is the last element in the list of list and I don't know why or how to fix this.

My expected output is

Dupable, 246, Mercedes, 25200
Quilani, 240, Kanna, 21000
...
KanaoSayo, 245, Kanna, 23000

Upvotes: 2

Views: 3990

Answers (4)

Neeraj
Neeraj

Reputation: 997

The Simplest solution I could think of:

    lucidData = [['Dupable', '246', 'Mercedes', '25200'], ['Qilani', '240', 'Kanna', '21000'],
                 ['MihKyle', '250', 'Mihile', '34750'], ['Goobtella', '245', 'Kanna', '27000'],
                 ['Gosenka', '240', 'Buccaneer', '25762'], ['Eeldu', '244', 'Pathfinder', '33359'],
                 ['YoungHanny', '249', 'Dual Blade', '36000'], ['Yumiikko', '248', 'Kanna', '32721'],
                 ['lRollingDeep', '247', 'Adele', '29201'], ['LordPrime', '247', 'Kaiser', '33000'],
                 ['GoiabaSama', '255', 'Demon Avenger', '964310'], ['GordoChorizo', '246', 'Dual Blade', '30387'],
                 ['ScarletReyne', '251', 'Ark', '29651'], ['StupidGameu', '251', 'Demon Avenger', '31674'],
                 ['TURTLESmrff', '242', 'Pathfinder', '2400'], ['Sleepybearrx', '240', 'IL Mage', '27953'],
                 ['Wremy', '251', 'Hero', '35773'], ['woele', '245', 'Phantom', '33578'],
                 ['Codé002', '240', 'Kanna', '22219'], ['FullSword', '250', 'Adele', '29548'],
                 ['Ch0senAlpha', '242', 'Hero', '28521'], ['MeserMule', '254', 'Kanna', '33974'],
                 ['KanaoSayo', '245', 'Kanna', '23000']]
    
    
    def formatting(data):
        for i in data:
            print(', '.join(i))
    
    
    formatting(lucidData)

# OUTPUT:
Dupable, 246, Mercedes, 25200
Qilani, 240, Kanna, 21000
MihKyle, 250, Mihile, 34750
Goobtella, 245, Kanna, 27000
...............
MeserMule, 254, Kanna, 33974
KanaoSayo, 245, Kanna, 23000

Upvotes: 0

Matthias
Matthias

Reputation: 13222

You need to join the entries in the list with , and then join the result with newlines.

def formatting(data):
    result = []
    for entry in data:
        result.append(', '.join(entry))
    return '\n'.join(result)

Using a generator you can write this in one line.

def formatting(data):
    return '\n'.join(', '.join(entry) for entry in data)

Using an index to access every element of a list in a for loop is an anti-pattern in Python. That's why I replaced it and iterate over the elements themselves.

Upvotes: 2

Krishna Chaurasia
Krishna Chaurasia

Reputation: 9572

As already pointed by others, you override the datas object in each iteration while you should be appending to the result from the previous iteration:

def formatting(data):
  datas = ''
  for elt in data:
      datas = ', '.join(elt) + '\n' + datas
  return datas

Note: I have also modified the looping part to directly iterate over the element instead of index-based access of the list.

Upvotes: 0

gunjit bedi
gunjit bedi

Reputation: 60

The variable 'datas' is getting overwritten with every iteration of the loop. Hence you are getting the last value only.

Try below if you need one list of the all the elements.

datas = []
def formatting(data):
  for i in range(len(data)):
    datas.extend(, '.join(data[i]))
  return datas

print(formating(lucidData)

Upvotes: 0

Related Questions