Pythonic way of comparing all adjacent elements in a list

lorenzocastillo

I want to know if there's a more Pythonic way of doing the following:

A = some list
i = 0
j = 1
for _ in range(1, len(A)):
    #some operation between A[i] and A[j]
    i += 1
    j += 1

I feel like this should/could be done differently. Ideas?

EDIT: Since some are asking for requirements. I wanted a general-purpose answer. Maybe to check if A[i], A[j] are between a certain range, or if they're equal. Or maybe I wanted to do a "trickle-up" of elements. The more general, the better.

Tadhg McDonald-Jensen

zip lets you combine multiple iterators:

for i,j in zip(range(0,len(A)-1), range(1,len(A))):
    #some operation between A[i] and A[j]

you can also use enumerate on a range object:

for i,j in enumerate(range(1,len(A)):
    #some operation between A[i] and A[j]

Note that unlike the other answers this gives you access to the indices of A not just the items, this is necessary if you want to use any assignment to A[i] or A[j], for example here is a very basic bubble sort:

A = list(range(10))
found1=True
while found1:
    found1=False
    for i,j in enumerate(range(1,len(A))):
        if A[i] < A[j]:
            A[i],A[j] = A[j],A[i]
            found1=True
print(A)

this is only possible when you iterate over the indices of A.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

A Pythonic way to insert elements of a list into a list of lists?

From Dev

Pythonic way of getting hierarchy of elements in numeric list

From Java

Pythonic way to select list elements with different probability

From Dev

pythonic way to filter list for elements with unique length

From Dev

Pythonic way to creating a list of certain elements

From Dev

Return bool value in pythonic way comparing within list?

From Dev

Pythonic way of single list with all variations of sublists

From Dev

change all the list element to true in pythonic way

From Dev

Effective way. of comparing list elements in Java

From Dev

Efficient & Pythonic way of finding all possible sublists of a list in given range and the minimum product after multipying all elements in them?

From Dev

Comparing each adjacent values of the list

From Dev

Is there a way to get all permutations with the condition of unique adjacent elements?

From Dev

Need a better way to make the function acts on adjacent elements of a list

From Dev

The most pythonic way to slice a Python list every 100 elements

From Dev

Operate on a list in a pythonic way when output depends on other elements

From Dev

Is there a pythonic way to sample N consecutive elements from a list or numpy array

From Dev

Pythonic way of counting max elements by index in a dictionary with list values

From Dev

pythonic way to check if any elements of a list is present in a set

From Java

Generate all permutations of a list without adjacent equal elements

From Dev

Is there a way to name all the elements of list

From Dev

Identify index of all elements in a list comparing with another list

From Dev

Counting number of swaps by comparing adjacent elements in an array

From Python

What is the most Pythonic way to find all Nones in a list of lists?

From Dev

Pythonic way not working with list

From Dev

The "pythonic" way for expending a list

From Dev

Pythonic way of subclassing a list

From Dev

Given a dictionary of lists, is there a pythonic smart way of comparing the i-th element of each list and extract the maximum value?

From Java

Pythonic way to create a dictionary from a list where the keys are the elements that are found in another list and values are elements between keys

From Dev

Pythonic way of finding the overlapping elements

Related Related

  1. 1

    A Pythonic way to insert elements of a list into a list of lists?

  2. 2

    Pythonic way of getting hierarchy of elements in numeric list

  3. 3

    Pythonic way to select list elements with different probability

  4. 4

    pythonic way to filter list for elements with unique length

  5. 5

    Pythonic way to creating a list of certain elements

  6. 6

    Return bool value in pythonic way comparing within list?

  7. 7

    Pythonic way of single list with all variations of sublists

  8. 8

    change all the list element to true in pythonic way

  9. 9

    Effective way. of comparing list elements in Java

  10. 10

    Efficient & Pythonic way of finding all possible sublists of a list in given range and the minimum product after multipying all elements in them?

  11. 11

    Comparing each adjacent values of the list

  12. 12

    Is there a way to get all permutations with the condition of unique adjacent elements?

  13. 13

    Need a better way to make the function acts on adjacent elements of a list

  14. 14

    The most pythonic way to slice a Python list every 100 elements

  15. 15

    Operate on a list in a pythonic way when output depends on other elements

  16. 16

    Is there a pythonic way to sample N consecutive elements from a list or numpy array

  17. 17

    Pythonic way of counting max elements by index in a dictionary with list values

  18. 18

    pythonic way to check if any elements of a list is present in a set

  19. 19

    Generate all permutations of a list without adjacent equal elements

  20. 20

    Is there a way to name all the elements of list

  21. 21

    Identify index of all elements in a list comparing with another list

  22. 22

    Counting number of swaps by comparing adjacent elements in an array

  23. 23

    What is the most Pythonic way to find all Nones in a list of lists?

  24. 24

    Pythonic way not working with list

  25. 25

    The "pythonic" way for expending a list

  26. 26

    Pythonic way of subclassing a list

  27. 27

    Given a dictionary of lists, is there a pythonic smart way of comparing the i-th element of each list and extract the maximum value?

  28. 28

    Pythonic way to create a dictionary from a list where the keys are the elements that are found in another list and values are elements between keys

  29. 29

    Pythonic way of finding the overlapping elements

HotTag

Archive