List indexing and removing elements. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

List indexing and removing elements.

I was doing an exercise where to goal is to take one string and put all the caps characteres at the beginning of the string. My first solution was : y = 'AbDeULDSAF asdfGeeGGG' d = list(y) v = [] for e in d: h = d.index(e) if e != e.lower(): v.append(e) del d[h] p = v+d l = ''.join(p) print(l) Output : ADUDAGGGbeLSF asdfeeG So some of the cap character were missed. I guess that deleting a charactere in the list was modifying the indexing dynamically ( logical because if you remove an element the indexing change) I solved the issue with doing two new lists, one for the small charactere and one for the caps. y = input('enter a string') d = list(y) v = [] t = [] print(d) for e in d: h = d.index(e) if e != e.lower(): v.append(e) else: t.append(e) p = v+t l = ''.join(p) print(l) This time seems to be working fine. I am still looking at way to do this by creating only one list. I will try to stock the index value first, then in another loop use those index value to remove the elements in one go.

18th Nov 2019, 2:30 PM
nicolas seespan
nicolas seespan - avatar
16 Answers
+ 8
If you like you can use comprehension with 2 new lists merging together: y = 'AbDeULDSAF asdfGeeGGG' print(''.join([i for i in y if i.isupper()]+[j for j in y if j.islower()])) #output: "ADULDSAFGGGGbeasdfee"
18th Nov 2019, 2:43 PM
Lothar
Lothar - avatar
+ 5
Starting new ideas, i use only the initial list and than rearrange characters: y = 'AbDeULDSAF asdfGeeGGG' lst = list(y) x = 0 # counter for index iteration y = 0 # counter for insert position while x < len(lst): if lst[x].isupper(): lst.insert(y,lst.pop(x)) y += 1 x += 1 print(''.join(lst)) # output: "ADULDSAFGGGGbe asdfee"
19th Nov 2019, 9:41 AM
Lothar
Lothar - avatar
+ 4
This is my version: print(*sorted(input(), key=lambda l: l.islower()), sep=' ')
18th Nov 2019, 3:55 PM
HonFu
HonFu - avatar
+ 3
Do you need to keep the order of the letter in the list as they did chronologically in the string? If not, perhaps you can sort the list, then remove non alphabetical elements off the list.
18th Nov 2019, 2:41 PM
Ipang
+ 3
Following your second program, if you want only one list, for e in d: if e != e.upper(): v.append(e) else: v.insert(0,e) print(''.join(v)) But in the answer you can find more pythonic ways to do it.
18th Nov 2019, 6:41 PM
Bilbo Baggins
Bilbo Baggins - avatar
+ 2
Wow lothar that's good... I am a total newbie at coding, I just started for two or three weeks ago and I do this as a hobby. Right now I learned the basic of string, list and some if, else, for, while. I am focusing on going slowly but being sure that I understand the few elements I learned so fare. This means trying to solve the issue with the tool I currently have. What I am trying to do know is to create a list with the index value of the Caps character, then use this list after having reverse it, to remove them from the original list. If I remove the element starting from the end, the index of the next element to remove will not change. Well that's my idea. Ps : coding is fun :-)
18th Nov 2019, 2:51 PM
nicolas seespan
nicolas seespan - avatar
+ 2
So for what I undesrtand When I use the index function on my for loop and remove one elements, there is an actualization of the new index of the list e, So when the item in position [x] is removed, the element x+1 come in position x, but the for loop as already checked the elements in the x position and go directly to x+1, thus overlooking one element.
19th Nov 2019, 5:24 AM
nicolas seespan
nicolas seespan - avatar
+ 2
Yes, when you change a list while looping over it, it starts to move under your feet like an escalator, and you miss steps. One way to tackle this is to iterate over a copy of the list, so that it doesn't change: for element in your_list[:]: ... Another way is to move backwards through your list. That way, when you delete something, only the part of the list you don't need anymore changes. for i in range(len(d)-1, -1, -1): if d[i].islower(): d.append(d.pop(i))
19th Nov 2019, 8:32 AM
HonFu
HonFu - avatar
+ 2
So I was succefull in removing the capitalized character of the list using enumerate. I first created the reverse list "t" on the line under "v". It did not work, turned out that if you do this Python does not actualize the second list in real time. So I had to create t, after the first for loop. y = 'AbDeULDSAF aRFsdfGeeGGGG' d = list(y) v = [] print(d) for i,j in enumerate(d): if j != j.lower(): v.append(i) t = v[::-1] for x in t: g =d.pop(x) print(d) To be honest it was not what I was planning to do when I started my exercice, but it demonstrate that before trying to learn many tool, I would be better of trying to understand the few I have. Side not: coding is a lot of fun, more fun than video game, in fact. It was so interesting trying to understand why only some of the captialize letter were removed. Side not2 : I do not expect to learn python very fast. It give an illusion of being simple but in fact you need a lot of exposure to the code to understand all the nuance.
20th Nov 2019, 2:50 PM
nicolas seespan
nicolas seespan - avatar
+ 2
I agree everything you are saying and I like your deep and obstinate approach. Happy coding!
20th Nov 2019, 4:49 PM
Bilbo Baggins
Bilbo Baggins - avatar
+ 2
Thanks Bilbo. Coding is just a hobby for me. I am learning several languages (that's how I start to look at coding because the app was suggested). When I started Chinese, it looked so easy, next lesson, next lesson ect. Then you realize that it looked easy because you did not really learn. Can you really recognize this characters, can you recognize reproduce the tone. The answer is no of course, you can then get overwhelm by the quantity of thing you have to learn before being "basic". When I started sololearn I had the same feeling. You can for example finish the Python course very very fast. But can you really use the langage for coding? Answer no, because yes it sound easy but in practice there is a lot of info to understand. So I will stay on the string/list and basic manipulation before venturing forward. My main issue is time. I really do not have a lot of it, so I try to do a little every day, rather than a lot one day of the weeks. More efficient.
22nd Nov 2019, 1:45 PM
nicolas seespan
nicolas seespan - avatar
+ 1
Thanks for all the answer. Yesterday I tried to use more the index function, not working. So I created a list with the index value of the cap characters, printed it and I ended up with this. ['A', 'b', 'D', 'e', 'U', 'L', 'D', 'S', 'A', 'F', ' ', 'a', 's', 'd', 'f', 'G', 'e', 'e', 'G', 'G', 'G'] [0, 2, 4, 5, 2, 7, 0, 9, 15, 15, 15, 15] That's when I understood that I had seriously not understood what the index function what about. I understood : will give me the index of one element in the list. Reality : will give me the index of the first occurrence of one elements in the list. So now I really want to understand what happen in my first missed tried :-) and find the correct function for knowing the index of any elements.
19th Nov 2019, 12:28 AM
nicolas seespan
nicolas seespan - avatar
+ 1
You can use index by restarting the search right after the first find, in a loop (index allows you to give a starting point). It will all be more complicated than if you use the simpler tools Python provides. For example you can do it by filter: letters = 'cgGhnDhfZcvj' print( *filter(str.isupper, letters), *filter(str.islower, letters), ) Or comprehension: print( *[l for l in t if l.isupper()] +[l for l in t if l.islower()] ) And probably lots of other ways.
19th Nov 2019, 12:52 AM
HonFu
HonFu - avatar
+ 1
You have already answered to yourself: index gives you the first occurrence of an element, in case of repetitions. If you want the real index, use enumerate(): print(list(enumerate('aaa'))) -> [(0, 'a'), (1, 'a'), (2, 'a')]
19th Nov 2019, 5:05 AM
Bilbo Baggins
Bilbo Baggins - avatar
+ 1
Ok so I really wanted to understand WHY some cap character were taken but not some other. And WHY the last G was never converted. What I did was to use the same function but have it print the list each time one element was removed. ['A', 'b', 'D', 'e', 'U', 'L', 'D', 'S', 'A', 'F', ' ', 'a', 's', 'd', 'f', 'G', 'e', 'e', 'G', 'G', 'G'] ['b', 'D', 'e', 'U', 'L', 'D', 'S', 'A', 'F', ' ', 'a', 's', 'd', 'f', 'G', 'e', 'e', 'G', 'G', 'G'] ['b', 'e', 'U', 'L', 'D', 'S', 'A', 'F', ' ', 'a', 's', 'd', 'f', 'G', 'e', 'e', 'G', 'G', 'G'] ['b', 'e', 'L', 'D', 'S', 'A', 'F', ' ', 'a', 's', 'd', 'f', 'G', 'e', 'e', 'G', 'G', 'G'] ['b', 'e', 'L', 'S', 'A', 'F', ' ', 'a', 's', 'd', 'f', 'G', 'e', 'e', 'G', 'G', 'G'] ['b', 'e', 'L', 'S', 'F', ' ', 'a', 's', 'd', 'f', 'G', 'e', 'e', 'G', 'G', 'G'] ['b', 'e', 'L', 'S', 'F', ' ', 'a', 's', 'd', 'f', 'e', 'e', 'G', 'G', 'G'] ['b', 'e', 'L', 'S', 'F', ' ', 'a', 's', 'd', 'f', 'e', 'e', 'G', 'G'] ['b', 'e', 'L', 'S', 'F', ' ', 'a', 's', 'd', 'f', 'e', 'e', 'G'] Ok so there I notice a patter, when there is 2cap only the first one is removed, very visible in the UDLSAF sequence. I modified the string to test the theory. ['A', 'b', 'D', 'e', 'U', 'L', 'D', 'S', 'A', 'F', ' ', 'a', 'R', 'F', 's', 'd', 'f', 'G', 'e', 'e', 'G', 'G', 'G', 'G'] ['b', 'D', 'e', 'U', 'L', 'D', 'S', 'A', 'F', ' ', 'a', 'R', 'F', 's', 'd', 'f', 'G', 'e', 'e', 'G', 'G', 'G', 'G'] ['b', 'e', 'U', 'L', 'D', 'S', 'A', 'F', ' ', 'a', 'R', 'F', 's', 'd', 'f', 'G', 'e', 'e', 'G', 'G', 'G', 'G'] ['b', 'e', 'L', 'D', 'S', 'A', 'F', ' ', 'a', 'R', 'F', 's', 'd', 'f', 'G', 'e', 'e', 'G', 'G', 'G', 'G'] ['b', 'e', 'L', 'S', 'A', 'F', ' ', 'a', 'R', 'F', 's', 'd', 'f', 'G', 'e', 'e', 'G', 'G', 'G', 'G'] ['b', 'e', 'L', 'S', 'F', ' ', 'a', 'R', 'F', 's', 'd', 'f', 'G', 'e', 'e', 'G', 'G', 'G', 'G'] ['b', 'e', 'L', 'S', 'F', ' ', 'a', 'F', 's', 'd', 'f', 'G', 'e', 'e', 'G', 'G', 'G', 'G'] ['b', 'e', 'L', 'S', 'F', ' ', 'a', 'F', 's',
19th Nov 2019, 5:19 AM
nicolas seespan
nicolas seespan - avatar
+ 1
nicolas seespan you write: "So I will stay on the string/list and basic manipulation before venturing forward. " In my opinion you could learn more, and faster, and with more fun, looking also at other arguments. For example trying to understand the codes published everyday at SoloLearn: they are an invaluable source of knowledge.
22nd Nov 2019, 2:13 PM
Bilbo Baggins
Bilbo Baggins - avatar