-1

I am using a string.split(':') function so that my list consists of firstname:lastname pairs (e.g. ["John", "Doe", "Alex", "Jacobson" ...]

I know how to use a basic for loop where I would increment the index by two each time (and compare to the length of the list), but I want to take care of it in a more Python specific way.

Are there any cleaner looping constructs I can take which would allow me to consider the first two indices, and then the next two, etc?

AlwaysQuestioning
  • 1,464
  • 4
  • 24
  • 48
  • https://docs.python.org/2/library/functions.html#zip – asf107 Oct 26 '15 at 18:21
  • Possible duplicate of [What is the most "pythonic" way to iterate over a list in chunks?](http://stackoverflow.com/questions/434287/what-is-the-most-pythonic-way-to-iterate-over-a-list-in-chunks) and [alternative way to split a list into groups of n](http://stackoverflow.com/questions/1624883/alternative-way-to-split-a-list-into-groups-of-n/1625023) and [how do you split a list into evenly sized chunks in Python](http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python) – TessellatingHeckler Oct 26 '15 at 18:41

4 Answers4

3

Call iter on the list and zip:

l = ["John", "Doe", "Alex", "Jacobson"]

it = iter(l)

for f,l  in zip(it, it):
    print(f,l)

John Doe
Alex Jacobson

zip(it,it) just consumes two elements from our iterator each time until the iterator is exhausted so we end up getting the first and last names paired. It is somewhat equivalent to:

In [86]: l = ["John", "Doe", "Alex", "Jacobson"]

In [87]: it = iter(l)

In [88]: f,l = next(it),next(it)

In [89]: f,l
Out[89]: ('John', 'Doe')

In [91]: f,l
Out[91]: ('Alex', 'Jacobson')
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
1
for first,last in zip(string[::2],string[1::2]):

should work.

EDIT: sorry, I wasn't really being concise.

What zip does is create an object that allows two variables in a for loop. In this case, we are creating a zip object with a sliced list of every other object starting at index 0 (string[::2]) and a sliced list of every other object starting at index 1 (string[1::2'). We can then use two iterators (first,last) to iterate through that zip list.

ie.

>>> l1 = [1,2,3,4]
>>> l2= [5,6,7,8]
>>> zip(l1,l2)
<zip object at 0x02241030>
>>> for l,k in zip(l1,l2): print(l+k)
...
6
8
10
12
>>> for l,k in zip(l1,l2): print (l,k)
...
1 5
2 6
3 7
4 8
R Nar
  • 5,465
  • 1
  • 16
  • 32
1
>>> import itertools
>>> L = ["John", "Doe", "Alex", "Jacobson"]
>>> for fname, lname in zip(*itertools.repeat(iter(L), 2)): print(fname, lname)
...
John Doe
Alex Jacobson
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
0

You can use splicing, ie, Assuming all the names are in x

fname = x[::2]
lname = x[1::2]
y = []
for a,b in zip(fname,lname):
    y.append({'fname':a,
              'lname':b,})

Now you can easily iterate by,

for i in y:
    print i['fname'], i['lname']
sam
  • 2,033
  • 2
  • 10
  • 13
Yash Mehrotra
  • 3,032
  • 1
  • 21
  • 24