#πŸ”’ HOW DO I APPEND AN ITEM FROM A LOOPED LIST TO ANOTHER LIST

74 messages Β· Page 1 of 1 (latest)

modest oracle
#

I would like some help on how i can append ,lets say i from a loop for i in (alist)nums.

tiny epochBOT
#

@modest oracle

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.

gentle monolith
#

append works the exact same whether or not it's in a loop

modest oracle
tiny epochBOT
gentle monolith
#

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

modest oracle
#

ok i need to re think the whole thing right

gentle monolith
#

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

modest oracle
#

so if i want to append to another list i just put nums.append(evens)

gentle monolith
#

think about what each variable represents

#

evens and nums are both lists

#

do you want to append a list into a list?

modest oracle
#

ahh ok it needs to be evens.append(r), kinda right

gentle monolith
#

what is r?

modest oracle
tiny epochBOT
gentle monolith
#

you still have no indentation in your code

#

you need to be able to run your own code to test it

modest oracle
#

ok but i dont clearly understand wat your meaning by indention

gentle monolith
#

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)
deep heart
#

!indent

#

oh it has a cooldown

gentle monolith
#

This first example will not run, it will error. Are you testing your code as you go?

modest oracle
#

ok i am understanding

tiny epochBOT
gentle monolith
#

this looks the same as before

#

you have no indentation on any lines

modest oracle
#

no i moved the print 1 tab back

gentle monolith
#

maybe it's pasting weird?

#

Where are you writing your code?

modest oracle
deep heart
#

tha hell OS is that

deep heart
modest oracle
#

bro wat

gentle monolith
#

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)

modest oracle
#

#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)

tiny epochBOT
#

Hey @modest oracle!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
modest oracle
#

tack sΓ₯ mycket

gentle monolith
modest oracle
#

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

mighty light
#

Windows 7

modest oracle
#

bruh

#

linux

mighty light
#

Your mint look ahh

#

😭

modest oracle
#

bro am just getting it this things bro have mercy, but linux is linux bro

mighty light
#

Ok

gentle monolith
#

!e

shopping = ['apples', 'milk', 'bread', 'eggs']
for item in shopping:
    print(item)
tiny epochBOT
modest oracle
#

something to note ,i should always understand both the tools and the problem

gentle monolith
#

a for loop lets you access every element of an iterable (like a list)

modest oracle
#

ok ,thanks

gentle monolith
#

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

modest oracle
#

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

gentle monolith
#

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)
tiny epochBOT
modest oracle
#

i get thank you so much for the knowledge

tiny epochBOT
#
Python help channel closed for inactivity

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.