This question already has answers here:
Iterating over two lists one after another
(4 answers)
Closed 4 years ago.
I would like to create a list from elements of 2 different lists using list comprehensions.
For instance, assuming my 2 lists are men and women, I want a single list with all names:
men_names = [man.name for man in men]
women_names = [woman.name for woman in women]
all_names = men_names + women_names
Is there a one-line solution to obtain this list? Thanks!
EDIT: Using a list comprehension is a requirement because in my particular case it should be very much faster than building the list in a for loop, and performance is an issue.
Using itertools.chain is a way to achieve this without creating an intermediate list.
from itertools import chain
all_names = [person.name for person in chain(men, women)]
Related
This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 3 months ago.
Given the following dictionaries:
dict_first_attempt = {'Offense': ['Jack','Jill','Tim'],
'Defense':['Robert','Kevin','Sam']}
dict_second_attempt = {'Offense': ['Jack','McKayla','Heather'],
'Defense':['Chris','Tim','Julia']}
From this dictionaries, my focus is just the offense, so if I just wanted the list of those, I would do this:
first = dict_first_attempt['Offense']
second = dict_second_attempt['Offense']
For each of those lists, I am trying to create a code that can do the following:
Tell me all the possible combinations of first attempt offense and second attempt offense.
Outputs it in a list, with lists of the combinations.
The first element within the list has to be from the first attempt offense, and the second element has to be from the second attempt offense.
An example of the type of output I want is:
[['Jack','Jack'],['Jack','McKayla'],['Jack','Heather'],
['Jill','Jack'],['Jill','McKayla'],['Jill','Heather'],
['Tim','Jack'],['Tim','McKayla'],['Tim','Heather']]
import itertools
list(itertools.product(first, second))
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 10 months ago.
I have a code which takes input of the users and creates a nested list.
List = [list 1, list2, list 3,.....]
The list 1, list 2, list 3 and so on are dynamic and can keep increasing or decreasing.
So far my code output is:
All_data = [[a,b],[c,d],[1,2],[3,4],.....]
The output that I want (for the dynamic nested list):
All_data = [a,b,c,d,1,2,3,4,......]
Can anyone suggest me a solution to solve this?
You can use itertools.chain:
itertools.chain(*All_data)
Or:
itertools.chain.from_iterable(All_data)
This creates an iterator, if you want to get a list:
list(itertools.chain.from_iterable(All_data))
You can use numpy.concatenate(). It will condense the nested list into a single list.
If your nested list is dynamic and can grow exponentially, then I will suggest to use itertools.chain.from_iterable() from itertools library. It will be faster because it will not convert the list into a numpy array like the first option.
This question already has answers here:
Transpose list of lists
(14 answers)
Closed 12 months ago.
So lets say I have a hypothetical list of lists of file names in Python defined like so:
l = [["user1/stats1.csv", "user1/stats2.csv", "user1/stats3.csv"],
["user2/stats1.csv", "user2/stats2.csv", "user2/stats3.csv"]]
What would be the most pythonic way to group it by the number in statsN.csv such that the list would look like:
l = [["user1/stats1.csv", "user2/stats1.csv"],
["user1/stats2.csv", "user2/stats2.csv"],
["user1/stats3.csv", "user2/stats3.csv"],
For reference, the original list was obtained by using glob with the * wild card a la glob.glob("user1/stats*.csv") and glob.glob("user2/stats*.csv")
You could unpack the sublists and zip:
out = list(map(list, zip(*l)))
Output:
[['user1/stats1.csv', 'user2/stats1.csv'],
['user1/stats2.csv', 'user2/stats2.csv'],
['user1/stats3.csv', 'user2/stats3.csv']]
If you're using numpy, you can simply transpose:
>>> np.array(l).T.tolist()
[['user1/stats1.csv', 'user2/stats1.csv'],
['user1/stats2.csv', 'user2/stats2.csv'],
['user1/stats3.csv', 'user2/stats3.csv']]
This question already has answers here:
How can I find same values in a list and group together a new list?
(6 answers)
Closed 2 years ago.
I have a list like so:
[10,10,10,20,20,20,20,30,40,40,40]
I want to split into X amount of lists, where X = how many unique elements there are, in the case above there are 4. So I would want 4 lists like so:
[[10,10,10],[20,20,20,20],[30],[40,40,40]]
Might be a dumb question and there is an easy way to do this but any help is appreciated, language is python3.
itertools.groupby does what you need, except it returns iterators instead of lists. Converting to lists is easy though:
[list(g) for _, g in itertools.groupby(my_list)]
This question already has answers here:
Nested List and count()
(8 answers)
Closed 4 years ago.
I have a list with one list inside and I would like to count how many times is one element repeat. for example:
list = ['a','b','c',['a','d']]
find = 'a'
list.count(find)
The ouptput is 1, but I'm looking for 2.
There is any easy way to do it?
thanks
Archive with chain.from_iterable
from itertools import chain
print(list(chain.from_iterable(lst)).count('a'))
First make your list flatten and get the count.