Ad
How To Separate A Tuple Into Two Independant Lists?
I would like to separate the following tuple into two lists
(('happy', 5.001), ('neutral', 10.004), ('neutral', 15.006), ('happy', 20.071),
('fear', 25.071))
I want my lists to look like the following:
a = ('happy', 'neutral', 'neutral', 'happy', 'fear')
b = (5.001, 10.004, 15.006, 20.071, 25.071)
The split()
method is not working on this.
Ad
Answer
Here comes your new hero, the zip
function!
l = (('happy', 5.001), ('neutral', 10.004), ('neutral', 15.006), ('happy', 20.071), ('fear', 25.071))
a, b = zip(*l)
For future usages, we could say that it works in two different modes:
zip(*iterable)
generates n iterables (being n the size of every tuple in the iterable), where every iterable contains the ith element of each tuple (the example of my answer).zip(iterable_1, ..., iterable_n)
generates a single iterable where every element is a tuple of size n containing the element of every iterable at the corresponding index.
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad