2

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
3
  • 1
    Please post your expected output. Commented Apr 1, 2021 at 9:09
  • 2
    You overwrite datas in every iteration of your loop. Commented Apr 1, 2021 at 9:10
  • You should get a syntax error since your print is missing a closing brace, is this the real code? Commented Apr 1, 2021 at 9:14

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

0

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.

Comments

0

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

1 Comment

This works, but for my specific project it doesn't quite fit what I need. Thank you though!

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.