Reputation:
I want to multiply the values from a list and remove one value, the code is this:
list1 = [1,2,3,4]
times = 3
start = [1]
visited = [start]
list2 = [n for n in list1 * times]
list2 =
[1,2,3,4,1,2,3,4,1,2,3,4]
I want list2 to be the list1 multiply 3 times, but removing the value start, when I do this:
list2 = [(n for n in list1 * times) - start]
give an error, and
list2 = [(n for n in list1 * times) != start]
removes all the values start
and I do not want that.
How can I do that the list is multiplicated by a number and then just one valueremoved? The result would be :
list2 = [2,3,4,1,2,3,4,1,2,3,4]
Thank you very much!
**One more thing, [start] in this case is 1, but it could any number present in the list. Thank you!
Upvotes: 2
Views: 192
Reputation: 703
Remove first value from result list that is set in variable start
[n for i, n in enumerate(list1*times) if (i < len(list1) and n not in start) or (i >= len(list1))]
for
start = [1]
is result
[2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
for
start = [2]
is result
[1, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
etc.
Upvotes: 0
Reputation: 373
This should do the trick
>>>list1 = [1, 2, 3, 4]
>>>list2 = list1 + list1 + list1
>>>list2
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
>>>list2.pop(0)
1
>>>list2
[2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4
Upvotes: -1
Reputation: 16184
Use the normal multiplication operator, then the list.remove
method:
>>> list1 = [1,2,3,4]
>>> times = 3
>>> start = 1
>>> list2 = list1 * times
>>> list2.remove(start)
>>> list2
[2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
Upvotes: 0
Reputation: 865
Not sure if this is what you want
list1 = [1,2,3,4]
list2 = list1[1:] + list1*2
print (list2)
Upvotes: 0