How can I adjust the list to have some elements positioned on the same index?

Let’s say I have a list like this:

info = ['385395982', '3.1', 'This is dummy text', '3843955682', '2.0', 'This is also dummy text', '248298493', '4.2', 'Another dummy text for you'] 

As you can see, the list includes this information: timestamp, rating and some text. Each of the element is appended to the list. I would like to be able to access all timestamps at index 0, ratings at index 1, and texts at index 2. How should I modify the list in order to achieve that?

My code:

class Scraper():   review_information = []   review_date = ''   review_rating = ''   review_content = ''      def __init__():     ...     for contribution in review_json.get('contributions',[]):       self.review_date = contribution.get('sortTimestamp', '')       self.review_rating = contribution.get('rating', '')       self.review_content = contribution.get('text', '')       self.review_information.append(self.review_date)       self.review_information.append(self.review_rating)       self.review_information.append(self.review_content)    def parse(self):     for info in self.review_information:       item['review_date'] = self.review_information[0]       item['review_rating'] = self.review_information[1]       item['review_content'] = self.review_information[2] 

Bottom line, I would love to have all timestamps at index 0 so that in parse method I can access them with self.review_information[0] (and the same for rating and content).

The desired output of the list should be something like this:

In [16]: info[0]                                                                                                                                                                 Out[16]: ['385395982']          ['384395562']           ['248298493']  In [17]: info[1]                                                                                                                                                                 Out[17]: ['3.1']           ['2.0']           ['4.2']  In [18]: info[2]                                                                                                                                                                 Out[18]: ['This is dummy text']           ['This is also dummy text']           ['Another dummy text for you'] 

So that when I call the info[0] in the for loop, I get one timestamp at the time, not all timestamps at once. Does that make sense?

Thanks in advance!

Add Comment
1 Answer(s)

this will let you access all your entities using indices as a list

info = [info[::3], info[1::3], info[2::3]] 

Output:

In [16]: info[0]                                                                                                                                                                 Out[16]: ['385395982', '3843955682', '248298493']  In [17]: info[1]                                                                                                                                                                 Out[17]: ['3.1', '2.0', '4.2']  In [18]: info[2]                                                                                                                                                                 Out[18]: ['This is dummy text', 'This is also dummy text', 'Another dummy text for you'] 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.