ProgrammerForFun
ProgrammerForFun

Reputation: 17

Overwriting or updating string inside a list

I was given a simple task of writing a code to uppercase the odd indexed charachters of a string inside a list , to which i coded as follows :

list1 = []
x = int(input("Enter the size of list : "))
for i in range(x):
    temp = str(input("Enter the element you wish to insert : "))
    list1.append(temp)
for i in range(x):
    size = len(list1[i])
    for j in range(size):
        if j%2 == 0:
           list1[i][j].replace(list1[i][j],list1[i][j].upper())
print(list1)

but it doesn't seems to be working and on printing the list1 it simply returns the normal string inside list . Help a beginner out

Upvotes: 0

Views: 286

Answers (3)

U13-Forward
U13-Forward

Reputation: 71570

Why not try enumerate with list comprehension:

list1 = []
x = int(input("Enter the size of list : "))
for i in range(x):
    temp = str(input("Enter the element you wish to insert : "))
    list1.append(temp)
list1 = [''.join(x.upper() if not (idx % 2) else x for idx, x in enumerate(i)) for i in list1]
print(list1)

Example output:

Enter the size of list : 4
Enter the element you wish to insert : dog
Enter the element you wish to insert : cat
Enter the element you wish to insert : hen
Enter the element you wish to insert : rat
['DoG', 'CaT', 'HeN', 'RaT']

Upvotes: 2

Alex L
Alex L

Reputation: 157

I would improve the second loop by doing a stepped range and the problem with your code is that you don't overwrite the new string in the list. The replace function only returns the string with the replaced character, does not replace it in the original list.

I did an example using string slicing which might be simpler to understand.

for i in range(x):
    if len(list1[i]) == 0:
        continue
    for j in range(1, len(list1[i]), 2):
        list1[i] = list1[i][:j] + list1[i][j].capitalize() + list1[i][j+1:]

Upvotes: 1

user15801675
user15801675

Reputation:

You don't actually need the inner for loop. You can just check if i divided by 2 is equal to 1 and replace it.

list1 = []
x = int(input("Enter the size of list : "))
for i in range(x):
    temp = str(input("Enter the element you wish to insert : "))
    list1.append(temp)
for i in range(x):
    if i%2==1:
        list1[i]=list1[i].upper()

print(list1)

Example:

Enter the size of list : 4
Enter the element you wish to insert : a
Enter the element you wish to insert : b
Enter the element you wish to insert : c
Enter the element you wish to insert : d
['a', 'B', 'c', 'D']

If you want to change case of individual characters of elements, you can do the following:

list1 = []
x = int(input("Enter the size of list : "))
for i in range(x):
    temp = str(input("Enter the element you wish to insert : "))
    list1.append(temp)

for i in range(x):
    list1[i]=''.join(i.upper() if j%2==0 else i for j,i in enumerate(list1[i]))
print(list1)

Output:

Enter the size of list : 3
Enter the element you wish to insert : cat  
Enter the element you wish to insert : hen 
Enter the element you wish to insert : pig
['CaT', 'HeN', 'PiG']

Upvotes: 2

Related Questions