Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modifying Lists
#1
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'
Reply
#2
replaceList[3] = AlphaNum[i]
Reply
#3
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?
Reply
#4
if x == replaceList[...]
Reply
#5
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]
Reply
#6
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
Reply
#7
for i in range(0, currentIndex-1):
Reply
#8
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?
Reply
#9
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)
Reply
#10
(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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 3,328 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 5,098 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020