-1

I have a list:

List1 = ['name','is','JOHN','My']

I want to append the pronoun as the first item in a new list and append the names at last. Other items should be in the middle and their positions can change.

So far I have written:

my_list = ['name','is','JOHN','My']

new_list = []

for i in my_list:
  if i.isupper():
    my_list.remove(i)
  new_list.append(i)
print(new_list)

Here, I can't check if an item is completely upper case or only its first letter is upper case.

Output I get:

['name','is','JOHN','My']

Output I want:

['My','name','is','JOHN']

or:

['My','is','name','JOHN']

EDIT: I have seen this post and it doesn’t have answers to my question.

null_override
  • 467
  • 3
  • 10
  • 30
  • 3
    You should not modify a list that you're iterating over. – Barmar Sep 08 '20 at 20:24
  • on what condition are you sorting the list? – deadshot Sep 08 '20 at 20:25
  • 1
    `[t[1] for t in sorted((0 if s.istitle() else 2 if s.isupper() else 1, s) for s in List1)]` (and no I am not going to post it as an answer because it is not maintainable code even though it works...) – alani Sep 08 '20 at 20:26

3 Answers3

3

i.isupper() will tell you if it's all uppercase.

To test if just the first character is uppercase and the rest lowercase, you can use i.istitle()

To make your final result, you can append to different lists based on the conditions.

all_cap = []
init_cap = []
non_cap = []

for i in my_list:
    if i.isupper():
        all_cap.append(i)
    elif i.istitle():
        init_cap.append(i)
    else: 
        non_cap.append(i)

new_list = init_cap + non_cap + all_cap
print(new_list)

DEMO

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

How about this:

s = ['name', 'is', 'JOHN', 'My']

pronoun = ''
name = ''
for i in s:
    if i.isupper():
        name = i
    if i.istitle():
        pronoun = i

result = [pronoun, s[0], s[1], name]

print(result)
Porkfu
  • 71
  • 5
0

Don't @ me pls XD. Try this.

my_list = ['name','is','JOHN','My']
            
new_list = ['']

for i in range(len(my_list)):
  if my_list[i][0].isupper() and my_list[i][1].islower():
    new_list[0] = my_list[i]
  elif my_list[i].islower():
    new_list.append(my_list[i])
  elif my_list[i].isupper():
      new_list.append(my_list[i])
    
print(new_list)
  • You do the same thing in the `islower()` and `isupper()` cases. That won't reorder them. – Barmar Sep 08 '20 at 20:47
  • Did you try running it? XD ... just taking advantage of how code is read and implemented based on sequence...but then again, who am I to say...i'm still new to programming XD – Kirby Abogaa Sep 08 '20 at 20:49
  • 1
    Change the order of the input to `['JOHN','name','is','My']` and the result is `['My', 'JOHN', 'name', 'is']` – Barmar Sep 08 '20 at 20:53
  • All your code does is put the word with initial capital first, everything else is kept in the original order. – Barmar Sep 08 '20 at 20:53
  • You don't put the all-uppercase word last. – Barmar Sep 08 '20 at 20:53
  • but .append() DOES put the value in the last slot...doesn't it? which is why all upper-case word is in the last line of the code...it does re-order the sequence based on the problem presented – Kirby Abogaa Sep 08 '20 at 20:58
  • You're putting both `islower()` and `isupper()` in the last slot. – Barmar Sep 08 '20 at 21:06
  • 1
    You're not processing all the `islower()` first, you're processing them in the order that they appear in the input list. – Barmar Sep 08 '20 at 21:06
  • Try it with the altered input and you'll see. – Barmar Sep 08 '20 at 21:07