How can I split a list into smaller lists [duplicate] - python

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)]

Related

Combine Python lists with similar elements [duplicate]

This question already has answers here:
Interleave different length lists, elimating duplicates, and preserve order
(7 answers)
Closed 1 year ago.
I'd like to merge to lists with many similar elements preserving the order as much as possible.
So say for example we have this:
list1=[1,2,3,4,6,7,8,9]
list2=[1,2,3,4,5,8,10]
list1+list2=[1,2,3,4,6,5,7,8,9,10]
or maybe:
list1+list2=[1,2,3,4,5,6,7,8,10,9]
The idea is that 1,2,3,4 all match. Then we encounter 5 in list1 and 6 in list2 and we insert them next to each other in the merge list. If there are more elements in one list than the other then the extra rows are just appended to the end.
For unsorted output-
list(set(list1+list2))
For sorted output-
list(sorted(set(list1+list2)))

Split list element into lists in python [duplicate]

This question already has answers here:
Apply function to each element of a list
(4 answers)
Python Splitting Array of Strings
(6 answers)
Split each string in list and store in multiple arrays
(4 answers)
Closed 1 year ago.
I have a list
list = ['a:b:c:d', 'e:f:g:h', 'i:j:k:l']
What I'm trying to do is make a list of each of it's elements. Something like this:
listelement[0] = list[0].split(':')
print(listelement[0][1])
output: b
I can't seem to figure out how to create something that works similarly to this.
You can try list comprehension to do what you want.
new_list = [element.split(':') for element in list].
Also I would advise against the use of list as a name, since it's a reserved word.

count an element in a list with list inside in python [duplicate]

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.

Merge all items in a list [duplicate]

This question already has answers here:
Zip lists in Python
(10 answers)
Closed 4 years ago.
I'm having trouble figuring out how to merge two lists so that each item in each list is merged with the other one based on what place its in. Both of the lists are of equal length. For example:
xList=[abc,zxc,qwe]
yList=[1,2,3]
I need
[abc1,zxc2,qwe3].
I'm hoping I can make a loop that will be able to handle really long lists that will do this for me.
zip is your friend:
>>> xList=['abc','zxc','qwe']
>>> yList=[1,2,3]
>>> [x+str(y) for x,y in zip(xList,yList)]
['abc1', 'zxc2', 'qwe3']
Using map + lambda:
xList=['abc','zxc','qwe']
yList=[1,2,3]
print(list(map(lambda x,y: x+str(y),xList,yList)))
Output:
['abc1', 'zxc2', 'qwe3']

Successive iterations in one list comprehension [duplicate]

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)]

Categories