-1

I am creating a program that takes user input stores it in a list and then multiplies by either 5 or 1 alternating between each number. eg the first value is multiplied by 5 and the next 1 and so on. I want to remove all the values that i would multiply by 5 and add them to a separate list. How would this be done?

list1=[1,2,3,4]
list2=List1[???]
Tom
  • 43
  • 1
  • 13

2 Answers2

1

Here you go:

list1=[1,2,3,4]
list2 = [i*5 for i in list1[1::2]]

Two methods are used here slicing and list comprehension.

zipa
  • 27,316
  • 6
  • 40
  • 58
0

To get every other element, you could use a slice with a stride of two:

>>> list1 = [1,2,3,4]
>>> list2 = list1[::2]
>>> list2
[1, 3]

You can use a similar technique to get the remaining elements:

>>> list3 = list1[1::2]
>>> list3
[2, 4]

For further information, see Explain Python's slice notation

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012