Unpack list of list python [duplicate] - python

This question already has answers here:
Transpose/Unzip Function (inverse of zip)?
(14 answers)
Splitting a list of sequences into two lists efficiently [duplicate]
(2 answers)
Closed 1 year ago.
I know this has already been asked in other thread but I recall a neater way of doing this:
so here is a code that i know how to unpack things:
list = [[1, "tom"], [4, "jane"], [2, "peter"], [5.5, "Ursala"]]
points=[p for p, n in list]
name=[n for p, n in list]
So I know this is how I extract lists from list of list
but i remember there is some neater way of doing this.
It's something like.
points,name = list
it gives me however the error: too many values to unpack (expected 2)
is this method for dictionary? not applicable for list of list?

Using zip you get tuples, convert those tuples to list using map
* is used for unpacking the iterable
l = [[1, "tom"], [4, "jane"], [2, "peter"], [5.5, "Ursala"]]
points,name = map(list, zip(*l))
# just to point out numpy is not only for numbers
import numpy as np
l = [[1, "tom"], [4, "jane"], [2, "peter"], [5.5, "Ursala"]]
ln = np.array(l)
points, name = ln[:, 0], ln[:, 1]
PS: list is already reserved by python use some other unreserved name

Related

Python: Remove every first element in a 2D-List [duplicate]

This question already has answers here:
Get the nth element from the inner list of a list of lists in Python [duplicate]
(1 answer)
Slicing list of lists in Python
(6 answers)
Closed 5 months ago.
i have list in python. For example Test=[[1,1],[1,2],[1,3],[1,4]].
Now i would like to create a 1D-List by removing every first number to get this: [1,2,3,4].
My current Code works just fine, however it is definitely not the most pythonic code.
Could anyone give me a better code for the following? Perhaps a small explenation would be great, as i would like to understand how to programm in good pythonic code. :)
i=len(Test)
b=[]
a=0
for x in range (100):
Test[a].remove(Test[a][0])
b+=Test[a]
a+=1
print(b)
greeting, Dominik
Test=[[1,1],[1,2],[1,3],[1,4]]
# Loop through list using list comprehension and select the second
# item
Test2 = [i[1] for i in Test]
print(Test2)
Using a list comprehension:
test = [[1, 1], [1, 2], [1, 3], [1, 4]]
output = [x[1] for x in test]
print(output) # [1, 2, 3, 4]

How to return a list of lists from a list of strings with lists in the strings [duplicate]

This question already has an answer here:
python convert string of lists into a list of lists [duplicate]
(1 answer)
Closed 3 years ago.
This may have been answered before but it's hard to look up precisely. I have a list of strings where the strings have lists in them (or they would be lists if they were not in strings). I want to return a list of lists.
m,x,y,z may or may not have been defined already
Here is what I might start with:
old_list = ['[1,2,m]','[4.1,3.5,5]','[x,y,z]', '["t","u","v"]']
Here is what I would want (depending on whether m,x,y,z have been defined):
new_list = [[1,2,'m'],[4.1,3.5,5],['x','y','z'],['t','u','v']]
new_list2 = [[1,2,m],[4.1,3.5,5],[x,y,z],['t','u','v']]
Not sure what your problem is, it works for me...
x = 10
y = 11
z = 10
list_in = ['[1,2,3]','[4.1,3.5,5]','[x,y,z]', "['t','u','v']"]
list_out = [eval(elem) for elem in list_in]
print(list_out)
Output:
[[1, 2, 3], [4.1, 3.5, 5], [10, 11, 10], ['t', 'u', 'v']]
import ast
mylist = ['[1,2,3]','[4.1,3.5,5]','[x,y,z]', "['t','u','v']"]
mynewlist = [ast.literal_eval(i) for i in mylist]
Of course this example doesn't work exactly as-is, because you haven't defined x, y or z.

How do I add indefinite number of list together and output a single list? [duplicate]

This question already has answers here:
What is the best way to iterate over multiple lists at once? [duplicate]
(2 answers)
Closed 4 years ago.
The inputs will be lists (which is the number of list is indefinite), the function is supposed to iterate through all the index and add each value of the list for same index for all the inputted list mathematically together. The output will be the a list which consist of all the added values
For example:
lista = [1,2,3] listb = [2,3,5] listc = [-3,2,1]
outputlist = [0,7,9]
My function below is only able to add 2 list together, I want no restrictions as to how many list. How do I do that?
Thank you very much in advance
def listadd(a,b):
counter = 0
list = []
while counter < len(a):
list.append(a[counter]+b[counter])
counter += 1
return list
You can use map with zip:
def listadd_new(*lsts):
return list(map(sum, zip(*lsts)))
assert listadd([1, 2, 3], [4, 5, 6]) == listadd_new([1, 2, 3], [4, 5, 6])

Flatten a nested list using list unpacking in a list comprehension [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 4 years ago.
I saw this solution for merging lists such as a = [1,2,3], b = [4,5,6] using res = [*a, *b].
Assuming I have a list of sub-lists such as ls = [a,b] is it possible to do something like res = [*i for i in ls]?
That specific line is invalid since SyntaxError: iterable unpacking cannot be used in comprehension. Can something similar be done?
If not, How can I create easily a list that contains all elements in sub-lists?
using python 3.5.3
No, I don't believe they've added support for list unpacking inside a comprehension yet.
As an alternative, you can use itertools.chain:
>>> from itertools import chain
>>> list(chain.from_iterable([a, b]))
[1, 2, 3, 4, 5, 6]
Or, a nested loop list comprehension:
>>> [y for x in [a, b] for y in x]
[1, 2, 3, 4, 5, 6]

python: quick fix for a list of two lists into one list [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 7 years ago.
Trying to flatten a list of lists into one list of unique values:
[[1, 2, 3], [4, 5, 6], [6, 7, 8]] into [1,2,3,4,5,6,7,8] where the first list might contain N number of lists.
You could try it like this:
a = [[1,2,3,4], [5,6,7,8]]
nxtList = []
for i in a:
for j in i:
nxtList.append(j)
This won't guarantee that your values are unique, but if you know ahead of time that the list (a) always contains unique values then it won't be a problem.

Categories

Resources