SuperCell
SuperCell

Reputation: 241

How do you check the first letter of the first tuple in a list?

So i have something like this

userPassList=[('*username','password'),('username2','password2'),('username3','password3')]

and i want to search through the list for the username with * as the first character.

I was thinking something like:

i=0
while i < len(self.userPassList):
    if (self.userPassList[i][:1]=='*'):
        self.conn.sendall(self.userPassList[i][1:])
    i= i+1

but this isnt quite right. Any ideas or suggestions?

Edit: I don't have the password just the username.

Upvotes: 0

Views: 1599

Answers (5)

Alexander Reshytko
Alexander Reshytko

Reputation: 2246

This is how it looks in idiomatic functional python:

try:
   self.conn.sendall(next((name[1:len(name)] for name, _ in userPassList if name.startswith('*')))
except StopIteration:
   pass

Upvotes: 0

Back2Basics
Back2Basics

Reputation: 7806

This isn't going to produce what you think it will.

Try running sections of code all by itself. Make sure the line (starting with userPassList=...) is syntactically correct before writing a program that deals with it.

Here is why:

 username = 'me'
 username2 = 'me2'
 username3= 'me3'
 password = password2 = password3 = ''
 userPassList=[(*username,password),(username2,password2),(username3,password3)]
 print(userPassList)
 [('m', 'e', ''), ('me2', ''), ('me3', '')]

'*' in this case will give you an iterable and iterate through so that the tuple is longer than expected.

Perhaps you mean:

 username = '*me'
 username2 = 'me2'
 username3= 'me3'
 password = password2 = password3 = ''
 userPassList=[(username,password),(username2,password2),(username3,password3)]
 print(userPassList)
 [('*me', ''), ('me2', ''), ('me3', '')]

Then the rest of your programming efforts would make more sense.

Upvotes: 1

9000
9000

Reputation: 40894

This is how it looks in idiomatic Python:

# Loop through the list until first hit.
for username, password in userPassList:  # Unpack the tuple as we fetch it.
  if username.startswith('*'):  # No mucking with indexes.
    self.conn...  # whatever
    break # We only need the first username

Upvotes: 1

aghast
aghast

Reputation: 15310

You appear to not need an index variable. So use for ... in ... rather than while ... i+=1.

for tpl in list_of_tpls:

With that done, you'll have a tuple as your itervalue, so you can use i[0] just as you have been doing. You should probably go ahead and store that in a local variable, since you refer to it more than once. It'll be faster, and clearer.

    username = tpl[0]

Strings are treated as arrays/lists/tuples: they can be indexed. The way to check the value of the first character is either to use .startswith() or [0].

    if username.startswith('*'):
    # ... or ...
    if username[0] == '*':

You've got the rest, I think.

Upvotes: 2

Vince W.
Vince W.

Reputation: 3785

Well the i = i + 1 needs to be outside of the if block, and instead of [:1] you could just use [0]

Upvotes: 0

Related Questions