#πŸ”’ how can i use double index i need help for my project

213 messages Β· Page 1 of 1 (latest)

minor veldtBOT
#

@river barn

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.

hearty spire
#

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.

river barn
#

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

hearty spire
#

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 πŸ™‚

river barn
#

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(", ")?

hearty spire
#

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())
minor veldtBOT
hearty spire
#

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

river barn
#

it is giving me this error AttributeError: 'list' object has no attribute 'split'

hearty spire
#

yes, you can't .split on lists directly

#

you must use it on a string

#

even a list of strings won't work πŸ™‚

river barn
#

πŸ₯² sooo what do i do im confused

hearty spire
#

you would need to break out the for loop, to get at each individual string in the list, then apply it to that

hearty spire
river barn
#

well if i wanted to add different names i need to do this

hearty spire
#

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

river barn
#

ok but it made mike tyson how can i turn it into M.T

hearty spire
#

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?

river barn
#

let me see show you the project it might be easy

#

i need to do this

hearty spire
#

yes, so our task is to get the first letters. can you write a loop that does that?

river barn
#

yeah i need to use for loop

hearty spire
river barn
#

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

hearty spire
#

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?

river barn
#

πŸ₯²

hearty spire
#

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

river barn
#

really were? i only got one list one layer?

hearty spire
#

you did for friend in list_friends: print(friend) and got that

river barn
#

yeah it printed me a list

hearty spire
#

which means, that ["Mike Tyson", ...] is itself one element of list_friends

river barn
#

ohh ok how can i make it into two elements so i can use index

hearty spire
#

so, in list_friends there is a nested layer like I showed

hearty spire
#

instead, directly do

list_friends = input("Enter the names etc etc").split(", ")
#

then you will end up with just one layer of list

river barn
#

ok i did that but it printed me two times

hearty spire
#

you also have print("\n".join(list_friends)) πŸ™‚

river barn
#

yes ;-; so it can be down not stuck together

hearty spire
#

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?

river barn
#

initials idk about that my english knowledge is not that good tbh my bad πŸ₯²

hearty spire
#

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.

river barn
#

oh yeah i need to that

hearty spire
#

now, coming back to the .split I discussed earlier...

river barn
hearty spire
#

one step at a time

#

what should your first step be?

river barn
#

idk maybe try using index to get len letter i think

hearty spire
#

what do you mean by "get len letter"?

river barn
#

;-; nvm it doesn't exist

humble rune
hearty spire
#

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?

river barn
hearty spire
#

you're mentally splitting the word by spaces, isn't it?

river barn
#

yes or i use comma

hearty spire
#

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?

river barn
#

yeah

hearty spire
#

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.

river barn
#

ohhh does split work if it is empty like friend.split(" ")

hearty spire
#

yep!

river barn
#

ok then now it got turned into two elements now do i do friend([0][0])

hearty spire
#

you're on the right track. play with it and see.

river barn
#

TypeError: 'str' object is not callable 😭

#

what does callable

#

mean

hearty spire
#

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.

river barn
#

sooo print(friend[0][0])?

hearty spire
#

that will run, yes. but it's not the full solution.

river barn
#

bro im sooo close i got the first letter in the first name

hearty spire
#

one thing, doing friend.split(" ") doesn't change friend. it gives you a new list.

river barn
#

well im close but not the right second letter

hearty spire
#

you need to use the new list returned by splitting.

river barn
#

new list hmm

#

ok now the proplem is how do i make new list ;-;

#

do i do name_1.split(" ")?

hearty spire
#

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.

river barn
#

i dont understand?

river barn
hearty spire
#

you know how you did

list_friends = input("blah blah").split(", ")

?

river barn
#

yeah

#

oh no it has to be in one not two inputs

hearty spire
#

there, you stored the list given by split into a variable called list_friends

#

similar idea here

river barn
#

wait you sure i don't need use list to do that ;-;

hearty spire
#

I'm not sure what you mean by "use list to do that" πŸ™‚

river barn
#

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

hearty spire
#

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
river barn
#

nope still same thing ;-;

hearty spire
#

ah sorry

humble rune
river barn
#

let me show you it is easy showing instead of explaining

hearty spire
#
name_parts = "Mike Tyson".split()
print(name_parts[0][0])  # M
print(name_parts[1][0])  # T
#

corrected ^^

river barn
humble rune
#

!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)
minor veldtBOT
hearty spire
#

like nafez showed

river barn
#

and there is soo many stuff im confused

humble rune
river barn
#

but why using if statement here?

humble rune
humble rune
river barn
#

πŸ₯² 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

river barn
#

voice chat

humble rune
river barn
#

ok then it's okay bro

humble rune
river barn
#

i have too many questions ;-; like i got the names right but the initials still getting it wrong

river barn
#

list_friends=input("Enter the first and last name of your friend separated by a comma:").split(", ")
print("\n".join(list_friendsfriends))

humble rune
#

avoid input() for now, as this is confusing you.

river barn
#

really it is easy i think

humble rune
river barn
#

ok i did that now what list_friends=["Mike Tyson","John Brown"]
print("\n".join(list_friends))

river barn
#

hmm idk let me try something

humble rune
vale schooner
humble rune
vale schooner
#

Thats the basics i believe

humble rune
#

le my confused brain* what if the name consists of three words hhh

vale schooner
#

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

river barn
#

sorry im back from suffering ;-;

#

all my methods didn't work πŸ₯²

cyan elbow
#

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

minor veldtBOT
river barn
humble rune
river barn
#

😭 bruh

humble rune
river barn
#

;-; i got all them right i got only initials i need to do

cyan elbow
river barn
#

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

humble rune
humble rune
#

!paste

minor veldtBOT
#
Pasting large amounts of code

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.

river barn
#

i need to do this project

humble rune
#

you need to enter names at runtime instead of depending on a static list now, right?

river barn
#

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

humble rune
river barn
vale schooner
#

Show the code

river barn
#

ok give me min

river barn
#

nvm ;-; idk hindi

vale schooner
#

tafa Salah .. did you put that name for example?

river barn
#

no this is video i took

river barn
#

i added : to the loop no need to fix it

vale schooner
#

I think our conversion confused you.. you are trying to mix many methods.. the friend.split() is unnecessary

humble rune
river barn
#

idk someone told me to do that

humble rune
#

replace line 4-8 with this code

for friend in list_friends:
    abbreviation = ""
    for res in friend.split():
        abbreviation = abbreviation + res[0] + "."
    print(abbreviation)
river barn
#

well it worked but it has one problem

vale schooner
#

And what is that?

river barn
#

it shouldn't print like that

humble rune
vale schooner
#

That looks harderpithink

river barn
#

πŸ₯²

vale schooner
#

Make it more pythonic

river barn
#

huh pythonic???

vale schooner
#

Simple and clean

river barn
#

😭 bruh isn't python like easy language bruh why did it reach that level

humble rune
vale schooner
#

I think nested loops are too much for beginner

river barn
#

bro i still didn't take lesson about nested loops

humble rune
vale schooner
#

Have you learned multiple assignment thats the easiest way

river barn
#

;-; yeah alot

vale schooner
#

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

river barn
#

pratition idk what it do

minor veldtBOT
#
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.