Python Forum
Modifying Lists - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Modifying Lists (/thread-6320.html)

Pages: 1 2


Modifying Lists - RedSkeleton007 - Nov-16-2017

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'



RE: Modifying Lists - heiner55 - Nov-16-2017

replaceList[3] = AlphaNum[i]



RE: Modifying Lists - RedSkeleton007 - Nov-18-2017

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?


RE: Modifying Lists - heiner55 - Nov-18-2017

if x == replaceList[...]



RE: Modifying Lists - buran - Nov-18-2017

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]



RE: Modifying Lists - RedSkeleton007 - Nov-22-2017

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



RE: Modifying Lists - heiner55 - Nov-22-2017

for i in range(0, currentIndex-1):



RE: Modifying Lists - RedSkeleton007 - Nov-22-2017

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?


RE: Modifying Lists - heiner55 - Nov-22-2017

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)



RE: Modifying Lists - RedSkeleton007 - Nov-22-2017

(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?