How to clone a list within itself? [duplicate] - python

This question already has answers here:
Repeating list in python N times? [duplicate]
(4 answers)
Closed 12 months ago.
suppose I have a list
a = [1,2,4,5]
I want to create another list b which clones a and adds it 3 times. So b is expected to be
b = [1,2,4,5,1,2,4,5,1,2,4,5]
How do I do this easily?
Methods using pandas or numpy is also welcome.

#just use the * operator
b=a*3

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

concatenate two list without repeating elements [duplicate]

This question already has answers here:
Pythonic way to create union of all values contained in multiple lists
(7 answers)
How to get the union of two lists using list comprehension? [duplicate]
(2 answers)
Closed 2 years ago.
Given two list A and B, It the following the way to put all elements in teo set together avoiding repetitions?
[i for i in A \if i not in B]

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)

Dynamic arguments for Python functions [duplicate]

This question already has answers here:
Expanding tuples into arguments
(5 answers)
Closed 7 years ago.
I have a list of lists, and I would like to enter them into a function in such a way that each element of the main list is a different argument to the function.
squares = [[1,5,9,13], [2,6,10,14], [3,7,11,15], [4,8,12,16]]
print zip(squares[0], squares[1], squares[2], squares[3])
# displays [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
Is there a way to do something like print zip(arguments(squares)) so that the arguments can be entered dynamically?
This is what the * operator is for:
print zip(*squares)

how to define a list with predefined length in Python [duplicate]

This question already has answers here:
Create a list with initial capacity in Python
(11 answers)
Closed 9 years ago.
I want to do the following :
l = list()
l[2] = 'two'
As expected, that does not work. It returns an out of range exception.
Is there any way to, let say, define a list with a length ?
Try this one
values = [None]*1000
In place of 1000 use your desired number.

Categories