#๐Ÿ”’ how can i make it so every 2nd element of a list is part of another

6 messages ยท Page 1 of 1 (latest)

hardy sundialBOT
#

@primal mauve

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

compact cargo
#

Make a for loop running through your list, you can set it to loop through every second option or set a counter with some logic to check if current loop is able to divide by 2.
Within the loop extract current item from the list and add it to the other.

marsh wolf
#

the quickest way is to just slice the list for every 2nd element

your_list = [0,1,2,3]
other = your_list[1::2]```
#

no, that'd be your_list[2::2]

your_list[1::2] is 1, 3, 5, etc

the syntax is start:stop:step, empty stop meaning to the end

hardy sundialBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.