all possible sublists of a fixed length in python3 [duplicate] - python

This question already has answers here:
Find all possible sublists of a list
(6 answers)
Closed 4 years ago.
How can I generate all possible sublists of a given length from a list.
list=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
from this, I want to generate a list of length 5 with all possible unique combinations with no dupes within sublists.
sublists= [1,2,3,4,5], [2,4,5,6,7]....
Thanks
Suji

You can do it using itertools.combinations
import itertools
l=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
list(itertools.combinations(l, 5))
Also, don't use built-in such as list to name your variables.

Related

How to randomly select two elements of a python list? [duplicate]

This question already has answers here:
What does the random.sample() method in Python do?
(4 answers)
Closed 2 years ago.
mylist = ['a','b','c','d']
How can I index the list so that I choose only two of the four elements? mylist[] throws all sorts of errors depending what datatype I put in the square brackets for indexing
You can use random.sample Try this:
import random
mylist = ['a','b','c','d']
print(random.sample(mylist, 2))

How to chain individual elements of list into one individual element in Python [duplicate]

This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
I have the following list
List1 =['4','0','1','k']
How do i make sure that individual elements are combined into one single entity?
Here is the desired output
List1 =['401k']
Use str.join:
List1 = [''.join(List1)]

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.

How to create function that returns all combinations of this list in Python? [duplicate]

This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 4 years ago.
I have this kind of list:
[['I'],['want','love','like'],['cat',dog]]
and I want to create a function that can return all combinations of words in the lists like this:
[['I'],['want'],['cat']]
[['I'],['love'],['cat']]
[['I'],['like'],['cat']]
[['I'],['want'],['dog']]
[['I'],['love'],['dog']]
[['I'],['like'],['dog']]
PS: The function must work with any n words
import itertools
for k in itertools.product(*lst):
print(k)

sorting list in python accoeding to one dimension [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Sorting a tuple that contains tuples
hi
I have a list that goes A=[[a,'3'],[g,'1'],[y,2]]
I am looking for a quick way to arrange it according to the numbers (second dimension), so
NewA=[[g,'1'],[y,'2'],[a,'3']]
thanks
ariel
from operator import itemgetter
newA = sorted(a, key=itemgetter(1))

Categories