#π HOW DO I APPEND AN ITEM FROM A LOOPED LIST TO ANOTHER LIST
74 messages Β· Page 1 of 1 (latest)
@modest oracle
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.
can you give an example of what you're trying to do exactly?
append works the exact same whether or not it's in a loop
here
Please react with β
to upload your file(s) to our paste bin, which is more accessible for some users.
the syntax for appending is list_name.append(thing_to_append)
there's a lot of other stuff wrong here. You have no indentation, you're trying to index an int, you're needlessly converting a list into a list, you aren't returning
ok i need to re think the whole thing right
This doesn't give me any sense about what you know about python already so it's hard to guide you and your code in its current state
so if i want to append to another list i just put nums.append(evens)
think about what each variable represents
evens and nums are both lists
do you want to append a list into a list?
ahh ok it needs to be evens.append(r), kinda right
what is r?
like this
Please react with β
to upload your file(s) to our paste bin, which is more accessible for some users.
you still have no indentation in your code
you need to be able to run your own code to test it
ok but i dont clearly understand wat your meaning by indention
indentation is the spaces at the start of the lines. They are needed by python to determine what block of logic your code belongs to
for i in range(10):
print(i)
for i in range(10):
print(i)
This first example will not run, it will error. Are you testing your code as you go?
Please react with β
to upload your file(s) to our paste bin, which is more accessible for some users.
no i moved the print 1 tab back
here
tha hell OS is that
indentation looks correct
bro wat
yeah, I guess the way you're pasting it here is removing the indentation for some reason
everything looks ok except you need to return the list, not print it
(also it should be after the loop, not inside it)
#Question 1:
#Write a function that takes a list of numbers and returns a new list containing only the even numbers.
nums = [12, 45, 6, 68, 77, 98, 90]
def check_evens(nums):
evens = []
for i in list(nums):
if i % 2 == 0:
evens.append(i)
return evens
r = check_evens(nums)
print(r)
Hey @modest oracle!
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
tack sΓ₯ mycket
Do you understand what i is doing here?
for real i dont know and am just trying to build the problrm solving skill
i have been doing this like for a week now
bro am just getting it this things bro have mercy, but linux is linux bro
Ok
problem solving comes from understanding exactly what the tools are doing
!e
shopping = ['apples', 'milk', 'bread', 'eggs']
for item in shopping:
print(item)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | apples
002 | milk
003 | bread
004 | eggs
something to note ,i should always understand both the tools and the problem
a for loop lets you access every element of an iterable (like a list)
yes exactly
ok ,thanks
so here I'm using a for loop to access everything inside the shopping list, and I've assigned item to the loop
so as it goes through, item will be every item within the list
so its like a variable to acces the things in the list
you know from ai i thought that i is the only thing to use
I really recommend not using ai as a beginner
the variable name does not matter, just any variable name
!e
shopping = ['apples', 'milk', 'bread', 'eggs']
for blahblah in shopping:
print(blahblah)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | apples
002 | milk
003 | bread
004 | eggs
i get thank you so much for the knowledge
This help channel has been closed. 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.