Python Forum
Zip on single element in nested sequence.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Zip on single element in nested sequence.
#1
How do I create the list c=[2,3,4] from the list a=[[1,2,3],[2,3,4],[3,4,5]]? In other words, how do I create a list from element [1] of a list containing nested sequences?

I know b,c,d=zip(*a) produces b=[1,2,3], c=[2,3,4], and d=[3,4,5].  That works well, but I don't need b and d.  It seems I should be able to use zip(*a[1]) or something of the sort but I have no success.

This is a minor problem but I must be missing something obvious.

my code here
Reply
#2
a = [[1,2,3],[4,5,6],[7,8,9]]
b = zip(*a)[1]
print b
c =  [x[1] for x in a]
print c
Output:
(2, 5, 8) [2, 5, 8]
by the way, no need for zip for the unpacking:
b, c, d = a
EDIT: Above applies when you want the i-th element of each element in a. In this case i=1

If you need the 1st element of a:
b = a[1]
Output:
[4, 5, 6]
Reply
#3
The list comprehension works and is what I've used. However,zip(*a)[1] produces:

TypeError: 'zip' object is not subscriptable
Reply
#4
In Python 3, zip() doesn't return a list, it returns a specialized iterator. So you can't just ask for the nth item (1th item here). You'll have to operate on the iterator.
Reply
#5
In other words, list(zip(*a))[1].
Reply
#6
(Jun-30-2017, 07:00 PM)nilamo Wrote: In other words, list(zip(*a))[1].
That would be equivalent to Python 2, yes. Would be ok for small values of a. Unfortunately Python doesn't appear to have a very clean way of getting the nth value of an iterator so this is probably the cleanest thing to do.
Reply
#7
next() I guess?

So...
zipped = zip(*a) #maybe iter(zip(*a)), I didn't actually try
# ignore the first
next(zipped)
second_elem = next(zipped)
Reply
#8
Yeah, that would work for n=2, but it doesn't seem super elegant to me. I wish there was something in itertools to get the nth item of an iterator.
Reply
#9
compress?

compress(zip(*a), [0] * (n - 1) + [1])
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
With Toolz a little nicer.
pip install toolz
>>> from toolz import nth
>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>> nth(1, zip(*a))
(2, 5, 8)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: dictionary update sequence element #0 has length 1; 2 Jmekubo 4 35,607 Apr-28-2019, 07:25 PM
Last Post: Jmekubo
  Unable to locate element no such element gahhon 6 4,519 Feb-18-2019, 02:09 PM
Last Post: gahhon
  Using xpath to get value of a nested element name using tag named xq1xq1xq1 3 9,540 Jan-18-2019, 07:13 PM
Last Post: Larz60+
  Replace element in a nested list nagymusic 4 17,902 Nov-19-2018, 08:03 PM
Last Post: nilamo
  Change single element in 2D list changes every 1D element AceScottie 9 12,098 Nov-13-2017, 07:05 PM
Last Post: Larz60+
  Using nested for loop with a single list mikeavison 3 3,318 Aug-12-2017, 08:13 PM
Last Post: ichabod801
  Executing single unittest over a sequence volcano63 1 3,857 May-11-2017, 11:11 AM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020