1

I am trying to expand the following list
[(1, [('a', '12'), ('b', '64'), ('c', '36'), ('d', '48')]), (2, [('a', '13'), ('b', '26'), ('c', '39'), ('d', '52')])]

to

[(1,a,12),(1,b,24),(1,c,36),(1,d,48),(2,a,13),(2,b,26),(2,c,39),(2,d,52)]

I used zip(itertools.cycle()) in python 3, but instead get a zip object reference. Is there any other way I can do it? This worked for python 2

Parker_d
  • 85
  • 8
  • 5
    zip (and map) in python3 returns an generator. use `list(zip(...))` should fix it – Patrick Artner Oct 10 '18 at 18:22
  • 1
    I would have just done `[(n,) + tup for n, tuples in thing for tup in tuples]`. – Steven Rumbalski Oct 10 '18 at 18:27
  • 1
    @PatrickArtner nitpick: neither `zip` nor `map` return a generator, although, they return iterators. – juanpa.arrivillaga Oct 10 '18 at 18:54
  • lst_source = [(1, [('a', '12'), ('b', '64'), ('c', '36'), ('d', '48')]), (2, [('a', '13'), ('b', '26'), ('c', '39'), ('d', '52')])] lst_result = [(val3[0],val[0],val[1]) for val3 in lst_source for val in [ val2 for val1 in lst_source for val2 in val1[1]]] – Veera Samantula Oct 10 '18 at 18:55
  • @juanpa.arrivillaga I stand corrected - they produce iterators. and ranges are immutable sequences - gotta get that into my head one time. – Patrick Artner Oct 10 '18 at 18:58

2 Answers2

0

A zip object is iterable. Much like other APIs in Python 3, this one now returns an iterable object (like a generator) that lazily evaluates its input, rather than returning the entire result in a list.

In most cases, you can use the object in the same way as you did before.

If you need a list, simply call list() on the zip object:

result_list = list(zip(itertools.cycle(...)))
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
0

Provided that the zip object is created correctly, you can either do list(zip_object) or [*zip_object] to get the list.

Danyal
  • 528
  • 1
  • 7
  • 17