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!
You can get the l2 as follows:
l2 = (l1 * 3)[1:]
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.
Not sure if this is what you want
list1 = [1,2,3,4]
list2 = list1[1:] + list1*2
print (list2)
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]
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
Related
#Write a function named add_one_to_all that takes in a list of numbers and adds one to each of the original numbers
assert add_one_to_all([0, 0, 0]) == [1, 1, 1]
assert add_one_to_all([1, 2, 3]) == [2, 3, 4]
assert add_one_to_all([6, 7, 8]) == [7, 8, 9]
I've tried this
def my_list ([x,y,z]):
new_list = [x+1 for x in my_list]
def add_one_to_all(my_list):
return [x+1 for x in my_list]
def add_one_to_all(integer):
return integer+1
int_list = [1, 2, 3, 4, 5]
# Adding one on each integer
# using list comprehension.
output_list = [add_one_to_all(i) for i in int_list]
print(output_list)
The output will be [2,3,4,5,6]
You can use map
l = [1, 2, 3]
print(list(map(lambda x:x+1, l)))
result
[2, 3, 4]
I have two lists:
List1=[2,4,3]
List2=[1,2,1,3,2,1,5,4,1]
Need to generate output like this:
ResultList=[[1,2],[1,3,2,1],[5,4,1]]
Need help!!!
The below should work
list1 = [2,4,3]
list2 = [1,2,1,3,2,1,5,4,1]
offset = 0
data = []
for x in list1:
data.append(list2[offset:offset+x])
offset += x
print(data)
output
[[1, 2], [1, 3, 2, 1], [5, 4, 1]]
You can try this -
result_list=[]
a = 0
for i in list1:
result_list.append(list2[a:a+i])
a += i
from itertools import islice
lengths = [2, 4, 3]
numbers = iter([1, 2, 1, 3, 2, 1, 5, 4, 1])
lists = [list(islice(numbers, length)) for length in lengths]
print(lists)
Output:
[[1, 2], [1, 3, 2, 1], [5, 4, 1]]
>>>
I want to duplicate an specific element as many times as indicated.
The original list would look like this:
list=[1,2,3,4,5,6,7,8,9]
And here would have to be the duplicator and the element of the list that we want to duplicate.
times=4
num=4
The final list would have to look like this:
list=[1,2,3,4,4,4,4,5,6,7,8,9]
There are many ways to do it and if your list only have one occurrence of the number this is likely the simplest way
lst = [1,2,3,4,5,6,7,8,9]
num = 4
times = 4
ix = lst.index(num)
lst[ix:ix+1] = [num] * times
You can simply use list repetition and concatenation:
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
times = 4
num = 4
ind = lst.index(num)
result = lst[:ind] + [num] * times + lst[ind + 1:]
print(result)
[1, 2, 3, 4, 4, 4, 4, 5, 6, 7, 8, 9]
You can try with this:
ex_list = [1,2,3,4,5,6,7,8,9]
times=4
num=4
new_list = sorted(ex_list + [ex_list[ex_list.index(times)]] * (num-1))
print(new_list)
Output:
[1, 2, 3, 4, 4, 4, 4, 5, 6, 7, 8, 9]
This question already has answers here:
How do I reverse a part (slice) of a list in Python?
(8 answers)
Closed 3 years ago.
I have 2 test [0, 2] and [3, 4]
and my list is [1, 2, 3, 4, 5]
how can I reverse only in range from 0-2 and 3-4 to my list
[1, 2, 3, 4, 5] -> [3, 2, 1, 4, 5] -> [3, 2, 1, 5, 4]
this is my code. It only works in first time, and second time it not works!
How can I fix it?
def solution(arr):
test1 = [1, 3]
test2 = [4, 5]
totalTest = [test1, test2]
print(arr)
for x in totalTest:
a = []
for i in x:
a.append(i-1)
lenght = (a[1] - a[0] + 1)/2
index = a[1] - a[0]
# print(lenght)
for i in range(a[0], lenght): # i is the low index pointer
arr[index], arr[i] = arr[i], arr[index]
index -= 1
print(arr)
arr = [1, 2, 3, 4, 5]
solution(arr)
The above code outputs the result:
[3, 2, 1, 4, 5]
[3, 2, 1, 4, 5]
You can use list slicing as follow:
keep in mind that the lower bound is inclusive, but the upper bound is not
so if you want to slice a list from index 0 to index 2 you would do mylist[0:3]
mylist = [1, 2, 3, 4, 5]
mylist = mylist[0:3][::-1] + mylist[3:5][::-1]
# [3, 2, 1, 5, 4]
You can make a more general function to do this:
def reverse_parts(L, ranges):
""" Reverse the parts of list L indicated by pairs of indices in
list ranges
"""
for start, end in ranges:
L = L[:start] + L[start:end+1][::-1] + L[end+1:]
return L
print(reverse_parts([1,2,3,4,5], [(0, 2), (3, 4)])) # [3,2,1,5,4]
Alternatively, to reverse the list in-place:
def reverse_parts(L, ranges):
for start, end in ranges:
L[start:end+1] = reversed(L[start:end+1])
my_list = [1,2,3,4,5]
reverse_parts(my_list, [(0, 2), (3, 4)])
print(my_list)
You should break your list in two and reverse each part separately:
li = [1,2,3,4,5]
print(li[0:3][::-1] + li[3:5][::-1])
cheers!
For example I have a list:
L = [1, 2, 2, 3, 1, 1, 6, 10, 1, 3]
And I want to remove all 1's from the list, so that I would get:
L = [2, 2, 3, 6, 10, 3]
I tried iterating over the list and then deleting the element if it equals the element I want to delete (1 in this case), but turns out you can't iterate and delete stuff from a list at the same time since it messes up the counting. The best thing I've come up with is just construct a new list L2 that doesn't contain any of the 1's and then put that into L, but is there a solution that only involves mutating L?
but is there a solution that only involves mutating L?
You can rather iterate over a copy of your List - L[:], and remove element from L. That won't mess up counting.
If you really don't want to create a new list, you would have to iterate in reverse using range(len(L) - 1, -1, -1), but that won't be 'Pythonic' anymore.
>>> for x in L[:]:
... if x == 1:
... L.remove(x)
...
>>> L
[2, 2, 3, 6, 10, 3]
However, you can also use List Comprehension:
>>> L = [1, 2, 2, 3, 1, 1, 6, 10, 1, 3]
>>> L[:] = [x for x in L if x != 1]
>>> L
[2, 2, 3, 6, 10, 3]
Using the filter built-in:
>>> L = [1, 2, 2, 3, 1, 1, 6, 10, 1, 3]
>>> filter(lambda x: x is not 1, L)
[2, 2, 3, 6, 10, 3]
Or you can assign it back to L:
>>> L = [1, 2, 2, 3, 1, 1, 6, 10, 1, 3]
>>> L = filter(lambda x: x is not 1, L)
>>> L
[2, 2, 3, 6, 10, 3]
You can also wrap this concept into methods, to be able to specify a list of items to include/exclude:
def exclude(collection, exclude_list):
return filter(lambda x: x not in exclude_list, collection)
def include(collection, include_list):
return filter(lambda x: x in include_list, collection)
>>> L = [1, 2, 2, 3, 1, 1, 6, 10, 1, 3]
>>> L = exclude(L, [1])
>>> L
[2, 2, 3, 6, 10, 3]
If you don't want to mutate the list or generate any new copy of it. You can loop from end to begin and use index:
>>> for i in range(len(L)-1, -1, -1):
... if L[i] == 1:
... del L[i]
This is awkward to do in python without making a copy of the list...
This will do it without making a copy.
a = range(6) # Some array [0,1,2,3,4,5]
i=0
while i < len(a):
if a[i] == 4:
del a[i]
else:
i += 1
OUTPUT
>>> a
[0, 1, 2, 3, 5]