#π how can i use double index i need help for my project
213 messages Β· Page 1 of 1 (latest)
@river barn
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.
Hi, can you clarify what you mean by "double index"?
I guess you mean "two-dimensional" or nested lists, like
a = [[1, 2, 3], [4, 5, 6]]
print(a[0][2])
but I'm not sure, so I want you to clarify.
no for example i got mike tyson i need to make it M.T using index and i got list of names i need to do same as the example name
and using for loop i forgot to add that
Look into the str.split method. It'll let you split a string by whitespace, so "Mike Tyson" will become ["Mike", "Tyson"]. From there you can think about how to get the first letter from each element π
i will try that give me min to try it i will send the result if it worked
ok but how can i add the name of lists so it can know like do i do list_friends.str.split(", ")?
no, it would be something like: (one min)
!e
list_friends = ["Mike Tyson", "John Brown"]
for friend in list_friends:
print(friend)
print(friend.split())
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | Mike Tyson
002 | ['Mike', 'Tyson']
003 | John Brown
004 | ['John', 'Brown']
str.split means that the .split method is available on strings
each element in the list_friends is a string, so we can use .split on them and get this
it is giving me this error AttributeError: 'list' object has no attribute 'split'
yes, you can't .split on lists directly
you must use it on a string
even a list of strings won't work π
π₯² sooo what do i do im confused
you would need to break out the for loop, to get at each individual string in the list, then apply it to that
OK, so list_friends is not actually a list of strings. it's a list of list of strings, see?
well if i wanted to add different names i need to do this
yes, but since you did the .split(", ") at the input stage, you already have the first and last name in two separate items
we don't need to .split anymore
ok but it made mike tyson how can i turn it into M.T
first, let's think about the data we're working with.
imagine the user enters Mike, Tyson into the input
your code does .split(", "), so we get ["Mike", "Tyson"]
now our task is to turn this into "M.T"
can you write a loop that, given a list like ["Mike", "Tyson"], prints the first letter of each string inside the list?
yes, so our task is to get the first letters. can you write a loop that does that?
yeah i need to use for loop
write it. then the next steps will be clearer.
but how im confused on how to make it to m.t like i got names already but not in square bracket
my last step to make M.T
you already have them in a list (i.e. "square bracket") because of the .split(", ") that you did, as I said
try this:
for friend in list_friends:
print(friend)
(in place of the loop you showed above)
what do you get?
right. so that is one element of your friends list, see?
so your list_friends actually looks like
[
[ "Mike Tyson", "Muhammad Ali", "John Cena"]
]
there's another layer of list involved
really were? i only got one list one layer?
you did for friend in list_friends: print(friend) and got that
yeah it printed me a list
which means, that ["Mike Tyson", ...] is itself one element of list_friends
ohh ok how can i make it into two elements so i can use index
so, in list_friends there is a nested layer like I showed
what I would say is: don't append to list_friends
instead, directly do
list_friends = input("Enter the names etc etc").split(", ")
then you will end up with just one layer of list
ok i did that but it printed me two times
you also have print("\n".join(list_friends)) π
yes ;-; so it can be down not stuck together
that's why it's printing twice
anyway, from here, can you work out how to turn each name string like "Mike Tyson" into initials?
initials idk about that my english knowledge is not that good tbh my bad π₯²
oh OK, that's fine. you don't need to apologize for it.
"initials" of a name just means the first letters of the name joined together. like M.T. for Mike Tyson, like you want.
oh yeah i need to that
now, coming back to the .split I discussed earlier...
but how π₯² im confused on it like i can do two dimensional but in one element it is confusing
idk maybe try using index to get len letter i think
what do you mean by "get len letter"?
;-; nvm it doesn't exist
I think itβd be helpful to deep dive into nested loops first @river barn
think, perhaps, about how you would solve this problem. if I gave you a random name and told you to abbreviate it, what are you doing in your head?
true but i still didn't reach that lesson
you're mentally splitting the word by spaces, isn't it?
yes or i use comma
so you just need to do the same in Python
how do you get the individual words out of the string "Mike Tyson" is the question then, right?
yeah
now, you've already used .split(", ") to turn "Mike Tyson, Muhammad Ali" into ["Mike Tyson", "Muhammad Ali"]. can you use something similar here as well? think about it.
ohhh does split work if it is empty like friend.split(" ")
yep!
ok then now it got turned into two elements now do i do friend([0][0])
you're on the right track. play with it and see.
friend([0][0]) is not directly valid...
to explain the error, "calling" is something you do to a function. for example, print is a function, that you call by adding parentheses like print("something").
so when you wrote friend([0][0]), because of the parentheses, you tried to call friend. but friend, being a str, is not callable.
sooo print(friend[0][0])?
that will run, yes. but it's not the full solution.
bro im sooo close i got the first letter in the first name
one thing, doing friend.split(" ") doesn't change friend. it gives you a new list.
well im close but not the right second letter
right, it's because of what I noted here ^^
you need to use the new list returned by splitting.
new list hmm
ok now the proplem is how do i make new list ;-;
do i do name_1.split(" ")?
well, you don't need to make it. friend.split(" ") already gives you the list.
you just need to use it.
store it in a variable, and then use it, for example.
i dont understand?
like i did it using name_2
you know how you did
list_friends = input("blah blah").split(", ")
?
there, you stored the list given by split into a variable called list_friends
similar idea here
wait you sure i don't need use list to do that ;-;
I'm not sure what you mean by "use list to do that" π
like i need to make list of names mike tyson, mohammad ali, john cena and by using split it could be two elements instead one so i can use index to do that getting first letter
yes, you need to do that
but as I said, split already gives you a list. you just need to store it in a variable, and then index into that variable.
name_parts = "Mike Tyson".split()
print(name_parts[0]) # M
print(name_parts[1]) # T
nope still same thing ;-;
ah sorry
Can you explain the problem once again?
let me show you it is easy showing instead of explaining
name_parts = "Mike Tyson".split()
print(name_parts[0][0]) # M
print(name_parts[1][0]) # T
corrected ^^
it will work if it is single name but i got mulitple names
!e
list_friends = ["Mike Tyson", "John Brown"]
for friend in list_friends:
tmp = ""
for short in friend.split():
if short:
tmp += short[0] + "."
print(tmp)
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | M.T.
002 | J.B.
that's where the loop comes in. you just need to run this inside the loop that goes over list_friends.
like nafez showed
huh what does tmp stand for π
and there is soo many stuff im confused
just a random variable name i picked, this doesn't matter
but why using if statement here?
You need to revise the basics π
there might be the case like list_friends = ["Mike Tyson", "John Brown", " "]list with an empty string, just ignore it
π₯² can we go vc perchance so you can explain it because it is confusing in many levels tbh
if you dont want to it's okay
vc perchance?
voice chat
voice chat sorry bro π
ok then it's okay bro
you can freely ask here, I'm available to help
i have too many questions ;-; like i got the names right but the initials still getting it wrong
How are entering names?
list_friends=input("Enter the first and last name of your friend separated by a comma:").split(", ")
print("\n".join(list_friendsfriends))
avoid input() for now, as this is confusing you.
really it is easy i think
instead this do
list_friends = ["Mike Tyson", "John Brown"]
ok i did that now what list_friends=["Mike Tyson","John Brown"]
print("\n".join(list_friends))
okay, what next u wanna know
hmm idk let me try something
sure
There is also multiple Assignment which you can use
!e
list_friends = ["Mike Tyson", "John Brown"]
for friend in list_friends:
a,b = list_friends.split()
print(a) # Mike
that's great, but difficult for beginners to understand it a, b = ...
Thats the basics i believe
maybe, but i believe this might confuse them π
le my confused brain* what if the name consists of three words hhh
We are not talking about edge cases here.. what if there is no space to begin with .. this code is not build for that
Bruh
!e if you just want to split once, then str.partition works, even for the edge case you mentioned ```py
forename, _, surname = 'Mike Tyson'.partition(' ')
print(forename)
forename, _, surname = 'Cara'.partition(' ')
print(forename)
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | Mike
002 | Cara
well maybe but the str.partition i didn't take lesson about his function
let's keep this thread specific to @river barn's problem, our edge cases will confuse him π
π bruh
yes
;-; i got all them right i got only initials i need to do
(feel free to ignore this; I clearly didn't read up enough :)
it's okay
but for new someone im trying to get Mike Tyson turn into M.T and it need to be in for loop and with different name
can you please share the code? Iβm not really getting it
Naah
!paste
So that everyone can easily read your code, you can paste it in this website:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
ok then give me min to show you the project im dealing with
i need to do this project
you need to enter names at runtime instead of depending on a static list now, right?
well i did that like i used input to write names and i got the first one right but the second one the initials are confusing
Everything looks fine here.
nafez do you mind asking are you arab by any chance? so i can explain to you in arabic if you want
Show the code
ok give me min
i know hindi/urdu π
nvm ;-; idk hindi
tafa Salah .. did you put that name for example?
no this is video i took
I think our conversion confused you.. you are trying to mix many methods.. the friend.split() is unnecessary
if im not wrong friend.slip(" ") return list?
Yes
idk someone told me to do that
that's why he is getting unexpected output
replace line 4-8 with this code
for friend in list_friends:
abbreviation = ""
for res in friend.split():
abbreviation = abbreviation + res[0] + "."
print(abbreviation)
well it worked but it has one problem
And what is that?
put the print outside the second loop
That looks harder
π₯²
Make it more pythonic
huh pythonic???
Simple and clean
π bruh isn't python like easy language bruh why did it reach that level
for res in friend.split():
abbreviation = abbreviation + res[0] + "."
print(abbreviation)
Yeah it is trust me
I think nested loops are too much for beginner
bro i still didn't take lesson about nested loops
As a beginner, writing clean code doesn't matter tbh
as i said hhh
Have you learned multiple assignment thats the easiest way
;-; yeah alot
Obv not you
Let me say this your indexing method was perfect only one line was unnecessary.. even if you leave it there it wouldnβt change anything
@river barn
You can either use multiple assignment
Or partition method if you donβt know about nested loops
pratition idk what it do
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.