0

Creating duplicate integers in list

Create a function that duplicates every integer in a list I have to write a function that accepts a list of integers as a parameter and replaces every element with two copies of itself. For example, if a list named nums stores [1, 2, 3], the call of stutter(nums) should change it to store [1, 1, 2, 2, 3, 3]. This is what I tried, but rather than creating and printing a new list (b), how do I change the values in the original lists that is passes in as a parameter? I'm just trying to modify the original list (a) but the error it's giving me is unsupported 'int' type error because it's reading my integer list as a string. def stutter(a): b = [] for i in a: b.extend([i, i]) return b stutter([1, 2, 3])

20th Mar 2018, 4:01 AM
Taylor Deal
Taylor Deal - avatar
2 ответов
+ 3
def stutter(l): return [y for x in zip(l, l) for y in x] stutter([1, 2, 3])
20th Mar 2018, 6:27 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
This does what your looking for: https://code.sololearn.com/ce7Ny5rxSJKx
20th Mar 2018, 5:10 AM
John Wells
John Wells - avatar