0

I have a an string and I need to get it into a format where I can iterate trough it:

[["","","",""],["","2","",""],["","","",""],["2","","",""]]

Can anyone help.

Rafael Marques
  • 1,501
  • 15
  • 23
  • 2
    Can you please be more specific? Show some code, input and expected output. What have you tried so far? SO is not a code factory to write your code for you... Please REALLY look at [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), [How do I ask a good question](https://stackoverflow.com/help/how-to-ask), and [Help Center](https://stackoverflow.com/help) – LeoE Feb 09 '20 at 00:25

3 Answers3

1

If you want to get the list inside that string, you can just use json to load its content:

import json


list_ = json.loads('[["","","",""],["","2","",""],["","","",""],["2","","",""]]')

And then you can iterate over list_ as you please

revliscano
  • 2,227
  • 2
  • 12
  • 21
0

I am not sure what your asking exactly, but if you want to find the index in which "2" is located you can do this, otherwise pls elaborate:

storeString = [["","","",""],["","2","",""],["","","",""],["2","","",""]]

for i in storeString:
   if "2" in i:
     print(i.index("2"))
de_classified
  • 1,927
  • 1
  • 15
  • 19
0

I think the easiest way you can do it is:

my_list = [["", "", "", ""], ["", "2", "", ""], ["", "", "", ""], ["2", "", "", ""]]
my_new_list = []

for a_list in my_list:
    my_new_list.extend(a_list)

for item in my_new_list:
    print(item)

And the output will be:

2






2

You can iterate the list and use append instead of using extend:

my_list = [["", "", "", ""], ["", "2", "", ""], ["", "", "", ""], ["2", "", "", ""]]
my_new_list = []

for a_list in my_list:
    for item in a_list:
        my_new_list.append(item)

for item in my_new_list:
    print(item)

And the output will be the same.

And as The Zach Man mentioned, you can see more examples here (How to make a flat list out of list of lists?)

Rafael Marques
  • 1,501
  • 15
  • 23