I have a list of dictionaries that looks like this:
someList = [{'a':3}, {'a':4}, {'a':6}]
Assuming that the length of someList is >1000 , what is the most efficient and pythonic way to obtain the following list of dictionaries:
someList = [{'prev_a':0, 'a': 3 , 'next_a': 4}, {'prev_a':3, 'a':4 , 'next_a' : 6}, {'prev_a':4, 'a': 6 , 'next_a': 0}]
Where:
next_ahas the value of a in the succeeding dictionary andprev_ahas the value of a in the preceding dictionaryfirst and last values for prev_a and next_a, respectively, can be 0
Update 1:
The number of variables, such as a can be more than one; the dictionary could look like this:
someList = [{'a':2,'b':4,'c':9}]
dict_key_values = ['a','c']
In this case the list I want to get should only feature the prev_ and next_ values of a and c
someList = [{'prev_a':4, 'a': 6 , 'next_a': 0, 'prev_c':4, 'c': 3 , 'next_c': 0, 'b':1}, ...]
(Without the use of Pandas)
Edit
My initial approach was to enumerate(someList), use someList[i-1]['a']and someList[i+1]['a'] to access the element before and after to get prev_a and next_a.
The problem with this approach is that when the number of variables such as a increase, the code becomes hard to manage