Python Forum

Full Version: Modifying Lists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Strings are immutable. Lists are not, so I'm trying to learn how to manipulate lists. First up, I want to replace a value at a specific index in my list:
#!/usr/bin/env python3
#ForumListQuestions.py

AlphaNum = "abcdefg0123456"
replaceList = ['a', 'b', 'c', 'd', 'e']
i = 2
replaceList.replace(replaceList[3],AlphaNum[i])
for x in replaceList:
    print(x)
I was expecting to print out:
a
b
c
c
e

But instead I got an error:
Error:
Traceback (most recent call last): File "I:/Python/Python36-32/SamsPrograms/ForumListQuestions.py", line 7, in <module> replaceList.replace(replaceList[3],AlphaNum[i]) AttributeError: 'list' object has no attribute 'replace'
replaceList[3] = AlphaNum[i]
Thank you heiner55. Next up, I need to detect if I've reached the last char in my list once the list finishes printing. The code is:
#!/usr/bin/env python3
#ForumListQuestions.py

AlphaNum = "abcdefg0123456"
replaceList = ['a', 'b', 'c', 'd', 'e']
i = 2
replaceList[3] = AlphaNum[i]
for x in replaceList:
    print(x)
    if replaceList[x] == replaceList(len(replaceList)-1):
       print("And we have just printed out the last char in replaceList.")
The error is:
Error:
a Traceback (most recent call last): File "I:\Python\Python36-32\SamsPrograms\ForumListQuestions.py", line 10, in <module> if replaceList[x] == replaceList(len(replaceList)-1): TypeError: list indices must be integers or slices, not str
Also notice that for some reason, it prints out 'a', the first element in the list, but doesn't print the rest. What's wrong?
if x == replaceList[...]
for x in replace_list:
    print(x)
print("And we have just printed out the last char in replace_list.")
if you loop over the list in order to print each element, you don't need to check that you reach the last element, see the code above.
The way you do it, will cause problem if there are repeating elements in the list
e.g.
replace_list = ['a', 'b', 'e', 'c', 'd', 'e']
finally, if you want to access list element use indexes and you can use negative indexes too, e.g. for the last element
last_element = replace_list[-1]
I need a way to replace all characters in my list before a specied element in replaceList, while leaving the rest of the characters to the right of the specified element alone. I'm having problems with my list object though. Here's the code:

#!/usr/bin/env python3
#ForumListQuestions.py

AlphaNum = "abcdefg0123456p"
currentIndex = 2
replaceList = ['a', 'b', 'c', 'd', 'e']
#i = 2
for i in replaceList(0,currentIndex-1):
    replaceList[i] = AlphaNum[0]
for x in replaceList:
    print(x)
Error:
Traceback (most recent call last): File "I:\Python\Python36-32\SamsPrograms\ForumListQuestions.py", line 8, in <module> for i in replaceList(0,currentIndex-1): TypeError: 'list' object is not callable
for i in range(0, currentIndex-1):
I changed the syntax like you told me to:

#!/usr/bin/env python3
#ForumListQuestions.py

AlphaNum = "abcdefg0123456p"
currentIndex = 2
replaceList = ['a', 'b', 'c', 'd', 'e']
#i = 2
for i in replaceList[0:currentIndex-1]:
    replaceList[i] = AlphaNum[0]
for x in replaceList:
    print(x)
But now it's complaining about string and list index values:

Error:
Traceback (most recent call last): File "I:\Python\Python36-32\SamsPrograms\ForumListQuestions.py", line 9, in <module> replaceList[i] = AlphaNum[0] TypeError: list indices must be integers or slices, not str
What's going on here? I know strings are immutable, but you should still be able to point to the index of each character in a string. Is there a way to fix this without converting AlphaNum into a list?
AlphaNum = "abcdefg0123456p"
currentIndex = 2
replaceList = ['a', 'b', 'c', 'd', 'e']
#i = 2
for i in range(0, currentIndex-1):
    replaceList[i] = AlphaNum[0]

for x in replaceList:
    print(x)
(Nov-22-2017, 03:37 AM)heiner55 Wrote: [ -> ]
AlphaNum = "abcdefg0123456p"
currentIndex = 2
replaceList = ['a', 'b', 'c', 'd', 'e']
#i = 2
for i in range(0, currentIndex-1):
    replaceList[i] = AlphaNum[0]

for x in replaceList:
    print(x)
The output was:
a
b
c
d
e

The output I was expecting is:
a
a
c
d
e

What's wrong?
Pages: 1 2