#๐Ÿ”’ Pick a fruit and remove all others in the list before that

181 messages ยท Page 1 of 1 (latest)

vocal terrace
#

I'm trying to make a simple script for some random exercise.

The exercise is:
Make a list of 1 fruit for each letter in the alphabet sorted alphabetically (Done by chatGPT)
then

  1. Pick a fruit at random
  2. Remove it and all fruits that come before it in the alphabet from that list
  3. Repeat 1-2

Before I attempt this I want to go through the program, see what I need, what I need to use, etc. Conceptualize it properly, then write code, instead of being clueless and starting to type random ideas in.

How should I start, what would be the first step here?

fast frigateBOT
#

@vocal terrace

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.

modest arrow
#

This can be almost entirely solved with slicing

#

the only thing you need to know is the index of the fruit you want to remove

#

then you can create a slice that says "everything up until this index"

vocal terrace
#

is that a function or method i can look up in the docs

modest arrow
#
data = ['a', 'b', 'c', 'd', 'e', 'f']
#

have a look at this list for example

#

if I just wanted the first 3 items, I can use the slice [0:3]

#

!e

data = ['a', 'b', 'c', 'd', 'e', 'f']
print(data[0:3])
fast frigateBOT
modest arrow
#

the syntax is [start:stop:step]

#

[0:3] slices starting at 0, and stopping just before 3

#

so we end up with the items from index 0, 1, 2

#

we can remove based on this slice by reassigning it to an empty list

#

!e

data = ['a', 'b', 'c', 'd', 'e', 'f']
data[0:3] = []
print(data)
fast frigateBOT
modest arrow
#

so the missing piece here is just knowing where you want to slice until

vocal terrace
#

that went too fast for me

modest arrow
#

the text isn't going anywhere, feel free to read it over at your own pace and let me know if you have any questions

vocal terrace
#

what does [0:3]do

#

it removes the items from that list?

modest arrow
#

Do you understand how we can access an index in a list?

vocal terrace
#

data[0:3] = [] i havent seen this expression yet

#

yes

modest arrow
#

!e

data = ['a', 'b', 'c', 'd', 'e', 'f']
print(data[0])
fast frigateBOT
modest arrow
#

this always gives us back a single thing

vocal terrace
#

why is there an emptry bracket i mean

modest arrow
#

[] is an empty list

vocal terrace
#

so you are putting 0:3 into an empty list

modest arrow
#

so data[0:3] = [] is basically saying "replace data[0:3] with emptiness"

vocal terrace
#

or rather wait

modest arrow
#

the other way around

vocal terrace
#

i didnt know that was possible

#

so its like removing?

modest arrow
#

we're assigning "emptiness" to that slice

#

!e

data = ['a', 'b', 'c', 'd', 'e', 'f']
data[0] = []
print(data)
fast frigateBOT
modest arrow
#

it works basically the same without slicing

#

here I replaced the item in index 0 with an empty list

vocal terrace
#

but emptyness means it loses these indices and shifts the other ones to the left afterwards?

modest arrow
#

yes ๐Ÿ™‚

vocal terrace
#

damn thats covenient

modest arrow
#

very

#

there's two really nice slice tricks you should learn as well

#

we can provide None as a slice index

vocal terrace
#

thats not something youd learn in a book i think

modest arrow
#

slicing is quite common as you start learning more about lists

#

!e

data = ['a', 'b', 'c', 'd', 'e', 'f']
print(data[3:None])
fast frigateBOT
modest arrow
#

using None as the "stop" value basically says "until the end"

vocal terrace
#

but there's also a function slice()

#

gotcha

modest arrow
vocal terrace
#

so now i just need to loop it basically

modest arrow
#

there's no loop necessary

#

oh wait

#

yes, if you want to repeat the removals

vocal terrace
#

yes

#

ill try to implement that into a loop, sth i should be able to

#

but the slicing trick is really neat thanks

modest arrow
#

another neat thing you can use it for is reverse stepping

#

!e

data = ['a', 'b', 'c', 'd', 'e', 'f']
print(data[None:None:-1])
fast frigateBOT
modest arrow
#

this says "from the start, until the end, but backwards"

vocal terrace
#

looks like reverse() to me

#

do you prefer doing this over the function?

modest arrow
#

now, we actually don't need None, but we can leave it empty

#

[::-1]

#

it looks a bit weird, but it works the same

vocal terrace
#

ive seen that

#

dont know where

modest arrow
vocal terrace
#

it didnt say empty is also None

#

but it makes sense that it is

modest arrow
#

slicing works on strings/lists/tuples/ranges

#

slicing a string returns a string

vocal terrace
#

as in generates a new variable you mean

modest arrow
#

!e

text = 'python is cool'
print(text[::2])
print(text[::-1])

fast frigateBOT
modest arrow
vocal terrace
#

i haven't really grasped what "return" means in these contexts

modest arrow
#

the result of a function

#

have you learned len()?

vocal terrace
#

yea

modest arrow
#

len() is a function that returns the length of an object

vocal terrace
#

i se

#

e

#

yeah that makes sense

modest arrow
#
size = len('abc')
#

when I do this

#

first python evaluates len('abc')

#

it get's the returned value of 3

#

then the 3 is assigned to size

vocal terrace
#

if you'd look into the code of the function (len) it'd have a "return" at the end right`?

modest arrow
#

yes

vocal terrace
#

so its input for other functions

modest arrow
#

we're not assigning "the function call" to size

#

we're assigning the returned value from calling that function

vocal terrace
#

makes perfect sense

modest arrow
#

!e

def foo():
    return 10

x = foo()
print(x)
fast frigateBOT
modest arrow
#

foo() evaluates to 10, then it gets assigned to x

#

!e

def foo():
    return 10

print(foo(), foo(), foo())
fast frigateBOT
vocal terrace
#

๐Ÿ‘

#

fully got it

#

perfectly explained, thanks ๐Ÿ˜„

modest arrow
#

!e

x = print(5)
fast frigateBOT
modest arrow
#

what do you think would happen if I did print(x) on the next line?

vocal terrace
#

let me think for a sec

#

so i try to visualize it as code

#

and just replace x with what it actually is

#

so it'd be print(print(5))

modest arrow
#

yes, the right side of the = is always evaluated first

vocal terrace
#

error

#

unless printing a print function is sth that works

modest arrow
#

we aren't printing a print

#

we're printing what print returns

vocal terrace
#

thought that might be an exception

#

so print returns a 5

modest arrow
#

print returns nothing

vocal terrace
#

but on screen instead

modest arrow
#

this is a gotcha for beginners, don't worry lemon_smirk

vocal terrace
#

yeah i figured lol

#

so it does what now?

modest arrow
#
def foo():
    return 10

x = foo()
#

look at this again

#

after the last line, x has no understanding of foo

#

it just says "give me the value you returned"

#

it's not remembering that x = foo()

#

it only knows x = 10

vocal terrace
#

i can see that yeah

modest arrow
#

so it's the same with x = print(5)

vocal terrace
#

so a function itself is nothing more than an elaborate variable?

modest arrow
#

print(5) does it's job by displaying 5 to the console

#

and then it returns None, which gets assigned to x

modest arrow
#

it can take inputs (parameters) and provide output (return)

vocal terrace
#

but ultimately its just a value or string etc.

#

or a list

modest arrow
#

yes, depending on what it returns

#

it's up to you really based on your needs

vocal terrace
#

yeah im just talking about the return thing, if it has one

modest arrow
#

if there's no return, it returns None

#
def foo():
    return 5
    return 10
    return 15

x = foo()
#

what about x here?

vocal terrace
#

my guess would be 15

modest arrow
#

good guess, but no

#

functions stop at their first return

vocal terrace
#

so all of them

modest arrow
#

it only returns 5

vocal terrace
#

ah i see

#

i knew that

#

but seeing it like this makes it unintuitive

#

but its good to see that once because than it makes more sense

modest arrow
#

you can often have many return paths in a function

#

depending on parameters

vocal terrace
#

that feels like a quiz that is actually very insightful

modest arrow
#

๐Ÿ™‚

#

making you think about code is one of the best ways to learn

vocal terrace
#

agreed

modest arrow
#

I spent a lot of time in these threads when I was first learning python

#

because I found that trying to explain something to someone else was a great way to learn, because I was also testing myself

vocal terrace
#

i fully agree with that as well

#

but i need to know sth to be able to teach about it too

#

but once i get to that point ill be here as well i think

modest arrow
#

hopefully ๐Ÿ™‚

vocal terrace
#

something im actually looking forward too alot

#

thanks a lot for your help so far

modest arrow
#

np!

#

feel free to give that looping code a try now

vocal terrace
#

i will

covert kiln
#

wow i just looked at the whole coversation and it is AWSOME Fashoomp you should seriously get a work as a teacher

modest arrow
#

but for now I'm happy with my work ๐Ÿ™‚

fast frigateBOT
#
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.