Put number in list python

Hamza Javed

I have data in text box like this?

12 14
13 15
16 17

i want to get them in list_comprehension like this

['12','14']
['13','15']
['16','17']

How can i iterate over the text box data and put them in list like this? Here is my code

    for w in form.vars.location_whitelist.split('\n'):
         line_loc = [x.split('\n')[0] for x in w]                                                                                  

         lac, cellid =line_loc[0] , line_loc[1]

         location_whitelist.append([lac , cellid])
TrebledJ

You seem to be over-complicating the lists. When iterating through the lines, w already represents each line. As a result, you only need to split w to get a list from each line.

Note that I will be using .splitlines() instead of .split('\n').

original_text = \
"""12 14
13 15
16 17"""

list_comp = []
for w in original_text.splitlines():
     list_comp.append(w.split())

print(list_comp)

### Output:
### [['12', '14'], ['13', '15'], ['16', '17']]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive