Unpacking a tuple of lists into two lists - python

I have a function which returns a tuple of two lists ( E.G return (["a", "b"], ["c"]) ).
I'm trying to append the two returned lists into two different lists.
I could do something like:
temp1, temp2 = functionCall()
list1.append(temp1)
list2.append(temp2)
But I'm trying to find some more elegant solution without temporary variables.
Some things I tried are using for loops and lambdas but couldn't find a satisfying solution.
I'm probably missing something obvious since I'm pretty inexperienced.
Can someone help me?

Would it be possible to change the function signature and send in the top level lists as arguments and retrieve the final appended lists out directly?
Something like:
functionCall(list1, list2)

edit: explain and format
This += with list1 is actually a function call to extend for you
list1 += functionCall()
list2.append(list1.pop())

Related

Python comparing two list elements in Python 3

I have a creative problem that I want to solve.
Let's say if I have two list as below. I want to compare if all elements in the req_param list are also in the full_list. I know it is easy to do the same using a for loop and getting the answer. But I am trying to figure out if there is a python3 in-built fxn to do the same..
req_param = ['ele1','ele2','ele3','ele4]
full_param = [['ele1','ele2','ele3','ele4','ele6']
During the comparison, I don't care if there are additional elements in full_param list. I just care that if full_param has all the elements of the req_param, then somehow I want to return it true else, I want to return it false.
Currently, this works with the for loop. But really think there should be an inbuilt fxn like compare. The most important part is that each element may not come in the same order, so I am ok to sort my list before passing it to a fxn...
As was mentioned there are several ways:
Use all(): if all(item in full_list for item in req_param):
Use set(): if set(req_param).issubset(set(full_param)):
I figured out a different way you can solve the problem.
You could just use set() and len() to solve the problem instead of for loop
Here's how:
r = ['ele1','ele2','ele3','ele4']
f = ['ele1','ele2','ele3','ele4','ele6']
print(len(set(r)-set(f))==0)
use all keyword , it returns True if all the conditions are satisfied else it returns False

Fastest way to calculate new list from empty list in python

I want to perform calculations on a list and assign this to a second list, but I want to do this in the most efficient way possible as I'll be using a lot of data. What is the best way to do this? My current version uses append:
f=time_series_data
output=[]
for i, f in enumerate(time_series_data):
if f > x:
output.append(calculation with f)
etc etc
should I use append or declare the output list as a list of zeros at the beginning?
Appending the values is not slower compared to other ways possible to accomplish this.
The code looks fine and creating a list of zeroes would not help any further. Although it can create problems as you might not know how many values will pass the condition f > x.
Since you wrote etc etc I am not sure how long or what operations you need to do there. If possible try using list comprehension. That would be a little faster.
You can have a look at below article which compared the speed for list creation using 3 methods, viz, list comprehension, append, pre-initialization.
https://levelup.gitconnected.com/faster-lists-in-python-4c4287502f0a

Could someone explain how this piece of Python code works? (sorted list with lambda function)

I was trying to sort a list of lists on the second item in each of the lists in my "unsorted list", and found this piece of code. It works, but even though I have read about lambda function I'm having some problems wrapping my head around how it works. Could someone explain how it works, and maybe give me some input if this is a good way of sorting a list of lists or if i should use a different approach. In advance, thanks!
sorted_list = sorted(unsorted_list,key=lambda l:l[1])
I don't recognize the language, but it seems that the code is sorting an array of arrays, using the element at index 1 as the sorting key.

Python inserting lists into a list with given length of the list

My problem is, that need a list with length of 6:
list=[[],[],[],[],[],[]]
Ok, that's not difficult. Next I'm going to insert integers into the list:
list=[[60],[47],[0],[47],[],[]]
Here comes the real problem: How can I now extend the lists and fill them again and so on, so that it looks something like that:
list=[[60,47,13],[47,13,8],[1,3,1],[13,8,5],[],[]]
I can't find a solution, because at the beginning i do not know the length of each list, I know, they are all the same, but I'm not able to say what length exactly they will have at the end, so I'm forced to add an element to each of these lists, but for some reason i can't.
Btw: This is not a homework, it's part of a private project :)
You don't. You use normal list operations to add elements.
L[0].append(47)
Don't use the name list for your variable it conflicts with the built-in function list()
my_list = [[],[],[],[],[],[]]
my_list[0].append(60)
my_list[1].append(47)
my_list[2].append(0)
my_list[3].append(47)
print my_list # prints [[60],[47],[0],[47],[],[]]

Sum across all list positions in a dictionary

This is a question and answer I wanted to share, since I found it very useful.
Suppose I have a dictionary accessible with different keys. And at each position of the dictionary I have a list of a fixed length:
a={}
a["hello"]=[2,3,4]
a["bye"]=[0,10,100]
a["goodbye"]=[2,5,50]
I was interested to compute the sum across all entries in a using only position 1 of their respective lists.
In the example, I wanted to sum:
finalsum=sum([3,10,5]) #-----> 18
Just skip the keys entirely, since they don't really matter.
sum(i[1] for i in a.itervalues())
Also as a side note, you don't need to do a.keys() when iterating over a dict, you can just say for key in a and it will use the keys.
You can use a.values() to get a list of all the values in a dict. As far as I can tell, the keys are irrelevant. a.itervalues() works by iterating rather than constructing a new list. By using this, and a generator expression as the argument to sum, there are no extraneous lists created.
I used list-comprehensions for my one line solution(here separated in two lines):
elements=[a[pos][1] for pos in a.keys()] #----> [3,5,10]
finalsum=sum(elements)
I'm happy with this solution :) , but, any other suggestions?

Categories