Link to home
Start Free TrialLog in
Avatar of Smanyx
Smanyx

asked on

Effective way to iterate over a python list

Dear Experts,

I have the following list :
>>> nenga = [(u' galleries; diagrams; charts; specific; pictures', ' Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.', 0.48097322768095152, [(27206294, 0.75551649763751838), (27206308, 0.75551649763751849), (27206366, 0.75551649763762147)]), (u' document; blocks; choice; dark; building', u' Auf gepfiffen spazieren wei plaudernd. Herd scho ehe dort sie lauf ihn lag erst ware. Tod loben kommt brief karte hut. Die neuen aus augen jeder sitte. En wenn pa vers grob orte hing um ab. Grundstuck mu verbergend zaunpfahle es drechslers handarbeit. Aus vertreiben ubelnehmen lie wer hinstellte uns sauberlich neidgefuhl. (2016); doi:17.2532/mt.2015.98.', 0.46888522930249621, [(27203444, 1.0), (27203555, 0.85551542136588746)])]

I can manipulate it to access elements like:

>>> nenga[0]
(u' galleries; diagrams; charts; specific; pictures', ' Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.', 0.4809732276809515, [(27206294, 0.7555164976375184), (27206308, 0.7555164976375185), (27206366, 0.7555164976376215)])
>>> nenga[1]
(u' document; blocks; choice; dark; building', u' Auf gepfiffen spazieren wei plaudernd. Herd scho ehe dort sie lauf ihn lag erst ware. Tod loben kommt brief karte hut. Die neuen aus augen jeder sitte. En wenn pa vers grob orte hing um ab. Grundstuck mu verbergend zaunpfahle es drechslers handarbeit. Aus vertreiben ubelnehmen lie wer hinstellte uns sauberlich neidgefuhl. (2016); doi:17.2532/mt.2015.98.', 0.4688852293024962, [(27203444, 1.0), (27203555, 0.8555154213658874)])
>>> nenga[0][3]
[(27206294, 0.7555164976375184), (27206308, 0.7555164976375185), (27206366, 0.7555164976376215)]
>>> nenga[0][3][0][1]
0.7555164976375184
>>> nenga[0][3][1][1]
0.7555164976375185
>>> nenga[0][3][2][1]
0.7555164976376215
>>> nenga[1][3]
[(27203444, 1.0), (27203555, 0.8555154213658874)]

I am more interested in the values in bold. I can manually access them as shown in the code above.
But what I am struggling with is to access these elements dynamically in a loop.
I thought of something along the lines of a double for loop for example that would:
  • for the outer loop, iterate over the number of available elements in my nenga list
  • for the inner loop, iterate over the number of elements in any given element of the nenga list
Something like:
for i in range(0, len(nenga)):
	for k in range(0, len(nenga[i])):
		print nenga[i][3][k][1]

Open in new window

or
>>> for i in range(0, len(nenga)):
	for k in range(0, len(nenga[i][k])):
		print nenga[i][3][k][1]

Open in new window


But I'm getting those IndexError: list index out of range errors ... Somehow I can think this straight.

Any help is greatly appreciated.
Thanks.
Avatar of MotKohn
MotKohn

try:
for a in nenga:
    print(a)

Open in new window

not sure what you are trying to do. So hard to help further.
SOLUTION
Avatar of pepr
pepr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Smanyx

ASKER

Thanks @pepr,

The only problem with this approach is that, I might be wrong, but I can't access all these elements individually. With what I want to do, accessing those elements by their index in the list seems the easier approach because I need for example to be able to assign those elements as values to some dictionary keys. That dictionary will then need to be appended to a list and the list will be passed on to a jinja2 template to be rendered on a web page.
Each nenga element in a different <div> container.
So, ideally, I would loop through my nenga list: for each element, i.e. nenga[0 ....n].
For nenga[0] for example, I will get all the strings i.e. nenga[0][0], nenga[0][1], nenga[0][2]  and assign them as values to some dictionary keys. Then I'll tackle the list (i.e. nenga[0][3]), for each element within that list, I need to access some other data structure to pass their relevant content as values to some keys in another dictionary which will also need to be appended to the list to be passed on to the template.
Brief, the template shall receive a list of huddles/clusters  like:
[{'key':"value", 'key': "value", 'key': "value", 'key': [{'key': "value", 'key': "value", 'key': "value", 'key': "value"}]}, {'key':"value", 'key': "value", 'key': "value", 'key': [{'key': "value", 'key': "value", 'key': "value", 'key': "value"}]}, {'key':"value", 'key': "value", 'key': "value", 'key': [{'key': "value", 'key': "value", 'key': "value", 'key': "value"}]}]

>> Do not use the range()
Could you please elaborate on why?
At least the index can be useful to refer to a particular element ...
Not sure how to proceed without indexes ...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Smanyx

ASKER

Thanks @pepr
This is very good. But I'm trying to figure out how to manipulate this items cluster by cluster.
For example when I do:
>>> for i, (s1, s2, num, lst) in enumerate(nenga):
    for k in range(0, len(lst)):
        print lst[k][1]

I get:        
0.755516497638
0.755516497638
0.755516497638
1.0
0.855515421366

But what I really want to get is two lots of numbers:
0.755516497638
0.755516497638
0.755516497638
in one cluster and
1.0
0.855515421366
in another.
The grouping is important as each cluster relates to a particular theme for example.


>>> for i, (s1,s2, num, lst) in enumerate(nenga):
      print lst
      
[(27206294, 0.7555164976375184), (27206308, 0.7555164976375185), (27206366, 0.7555164976376215)]
[(27203444, 1.0), (27203555, 0.8555154213658874)]

I want a clear separation between the first set of numbers:
[(27206294, 0.7555164976375184), (27206308, 0.7555164976375185), (27206366, 0.7555164976376215)]
and the second one:
[(27203444, 1.0), (27203555, 0.8555154213658874)]

So that I can work with each grouping individually.
How do I achieve that?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Smanyx

ASKER

Thank you very much.
Here is the hint how you can collect and process the cluster values:
def process_cluster_values(cluster_index, cluster_values):
    """Processing of the cluster values collected for the cluster index.
    
    Here only the print.
    """
    print 'Values to be processed for cluster', cluster_index, cluster_values    
       
      
for cluster_index, (s1, s2, num, lst) in enumerate(nenga):
    cluster_values = [n2 for n1, n2 in lst] # list of n2 for the cluster
    process_cluster_values(cluster_index, cluster_values)

Open in new window

The inner loop was replaced by so called list comprehension, which actually generates the list using the same loop (only the syntax is shorter, more readable when you get used to, and more efficient [optimized]). Then the above defined function is used to process the collected values.