user7096526
user7096526

Reputation:

remove value list python

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

Answers (5)

zelenyjan
zelenyjan

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

stamaimer
stamaimer

Reputation: 6475

You can get the l2 as follows:

l2 = (l1 * 3)[1:]

Upvotes: 2

panatale1
panatale1

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

Uriel
Uriel

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

Stavros Avramidis
Stavros Avramidis

Reputation: 865

Not sure if this is what you want

list1 = [1,2,3,4]

list2 = list1[1:] + list1*2

print (list2)

Upvotes: 0

Related Questions