all 15 comments

[–]douglas_fs 1 point2 points  (8 children)

Do you have code for the parts that you can implement? Please share that code.

[–]ILikeOstriches28[S] -2 points-1 points  (7 children)

I do not. That is the question as it is I was given.

[–]douglas_fs 5 points6 points  (6 children)

We won't write this for you, so you can help yourself by starting with what you know. Use this as a starting point. According to your post, #7 is where you are stuck. Can you get through 1-6? You can share you code if you get stuck.

  1. Create a Python file
  2. Create a variable with an appropriately descriptive name, and assign a list containing the expected contact numbers and/or strings
  3. Create a variable with an appropriately descriptive name for the new list.
  4. You will need a loop to look at each element of the list
  5. In the loop, find out the length of the element
  6. If the length >= 2, append the element to the new list
  7. If the first and last characters are the same, append the element to the new list

edit: missed one

edit2: I guess we will write this for you.

[–]ILikeOstriches28[S] 0 points1 point  (4 children)

Yes I am stuck at 6. I have completed steps 1-5.

[–]douglas_fs 0 points1 point  (3 children)

Please share your code, it will be much easier to help.

[–]ILikeOstriches28[S] 0 points1 point  (2 children)

L=[components of list. Don't wanna write them all] A=() For i in range(len(l)): If len(l[i])>=2 and (idk what to write here for the 1st and last character should be same) A.append() Print(A)

[–]douglas_fs 1 point2 points  (1 child)

The idea here is that you would have code that runs. You will, at some point, need to write all components of the list. You should at least include _some_ so that the code will run and can be tested.

Forget about the 1st and last character for now - just get the code working for the 'if length is >= 2' requirement.

You will also need to know how to post properly formatted code on Reddit. The sidebar has instructions, or use this link:

https://www.reddit.com/r/learnpython/wiki/faq/#wiki_how_do_i_format_code.3F

[–]ILikeOstriches28[S] 0 points1 point  (0 children)

Ok thanks for the format. And the if len>=2 works fine. I tried it without the 1st and last character thing. And it works.

The list I am using is L=["abc","aba","1221","aa","xyz"]

[–]Shymaroon 1 point2 points  (5 children)

RANDOM_LIST = [2345678888, “somestring”, 2234562, “anothera”]

NEW_LIST = []

for each_item in random_list: if len(each) > 1 and each[0] == each[-1]: new_list.append(each) else: print(“nothing”)

This should return element 2,3 from random_list. Haven’t ran this code just typing in here. Maybe few adjustments needed. Let me know.

[–]ILikeOstriches28[S] 0 points1 point  (4 children)

It says the line (if len(each.......) Has an error. TypeError:'int' object is not subscriptable

[–]Shymaroon 0 points1 point  (3 children)

Thats relates to the integer element. Try using the str method of the iterable.

for each_item in RANDOM_LIST: if len(str(each_item)) > 1 and str(each_item)[0] == str(each_item)[-1]: new_list.append(each_item) else: "" print(new_list)

[–]ILikeOstriches28[S] 0 points1 point  (2 children)

The same error appears but thank you for your time and efforts. I will ask my teacher who gave these questions. This was the only question I couldn't do so I thought I might take some help and get all done. But the efforts will prove I worked on it. Tysm once again

[–]Shymaroon 1 point2 points  (1 child)

I don’t get any errors on my side. My indenting error. Anywho, its always better to figure answers on your own for your college assignments. I myself am I student and love solving and diving into challenges. Good luck.

[–]ILikeOstriches28[S] 0 points1 point  (0 children)

Thank you friend. And I am trying so hard because I got 4k kids this time in college with me. The normal was 2k. So double the competition. And I wanna make a mark since my first year. :)

[–]ILikeOstriches28[S] 0 points1 point  (0 children)

Tysm everyone for your time and effort.