user2407199
user2407199

Reputation: 3

Python Iterate an integer as long as elements exist in a list

I have a list that looks like this:

data = [['info', 'numbers', 'more info'], ['info', 'numbers', 'more info'], ['..*this is dynamic so it could have hundreds*..']]

data is read in from a dynamic file and split up to be like this, so the number of elements are unknown.

What I am trying to do is rejoin the information with a ':' between the items and store it into a text file per line but the problem is with a loop to iterate through the data elements and increment an integer to be used on the data list.

here is a snippet:

#not sure what type of loop to use here
# to iterate through the data list.
saveThis = ':'.join(data[n])
file2.write(saveThis+'\n')

thanks

Upvotes: 0

Views: 248

Answers (5)

abarnert
abarnert

Reputation: 365807

If you really want "a loop to iterate through the data elements and increment an integer", that's exactly what enumerate is for:

for i, element in enumerate(data):
    # i is the integer, incremented each time through the loop

However, I don't see why you think you want that for your problem. (Maybe if you described the problem better, or just showed us the code you have and the part that you don't know how to write?)

Upvotes: 0

Henry Keiter
Henry Keiter

Reputation: 17178

Is this what you're looking for? Assuming you're not using that n for anything other than iterating over the data list, you can get rid of it altogether and do a nice little loop like this:

for item in data:
    saveThis = ':'.join(item)
    file2.write(saveThis + '\n')

You could condense it even more, if you felt like it, but I'd probably avoid that. Readability counts!

# Condensed to one line, but a little harder to read:
file2.write('\n'.join(':'.join(item) for item in data))

Upvotes: 1

Fredrik Pihl
Fredrik Pihl

Reputation: 45672

Do you mean like this:

In [5]: a
Out[5]: [['info', 'numbers', 'more info'], ['info', 'numbers', 'more info']]

In [6]: [item for sublist in a for item in sublist]
Out[6]: ['info', 'numbers', 'more info', 'info', 'numbers', 'more info']

In [7]: ":".join([item for sublist in a for item in sublist])
Out[7]: 'info:numbers:more info:info:numbers:more info'

Upvotes: 0

jwodder
jwodder

Reputation: 57510

If the integer would only be used as an index into data, you can just dispense with the integer entirely and iterate over the elements of data instead:

for datum in data:
    saveThis = ':'.join(datum)
    file2.write(saveThis + '\n')

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1122412

Flatten the list, then join. itertools.chain.from_iterable() does the flattening:

from itertools import chain

':'.join(chain.from_iterable(data))

This would put a : between all the items in all the sublists, writing them out as one long string.

Demo:

>>> from itertools import chain
>>> ':'.join(chain.from_iterable(data))
'info:numbers:more info:info:numbers:more info:..*this is dynamic so it could have hundreds*..'

If you need the sublist each to be written to a new line, just loop over data:

for sublist in data:
    file2.write(':'.join(sublist) + '\n')

or use a nested list comprehension:

    file2.write('\n'.join(':'.join(sublist) for sublist in data) + '\n')

Upvotes: 2

Related Questions