Python Forum
Appending inside elements of lists, in an exact location
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Appending inside elements of lists, in an exact location
#1
In Python, how would I append to an element inside a list in a particular location? Ex.) if I have the list ['*4','*3','*2','*1'] and I want to add the elements [a,b, c] in a cycle, in between. So the result would be ['*a4', '*b3', '*c2', '*a1'].
Reply
#2
Given that you have two lists, you can use zip to iterate over them in parallel, and then you can concatenate in a loop. e.g.
first = "123"
second = "abc"

for num, letter in zip(first, second):
    print(num + letter)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Appending lists into lists from function is not working for me BoredBannana 2 1,341 Oct-25-2024, 05:18 PM
Last Post: deanhystad
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 2,168 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 8,667 May-17-2022, 11:38 AM
Last Post: Larz60+
  Matching Exact String(s) Extra 4 3,144 Jan-12-2022, 04:06 PM
Last Post: Extra
  replace exact word marfer 1 8,790 Oct-11-2021, 07:08 PM
Last Post: snippsat
  List of lists - merge sublists with common elements medatib531 1 4,256 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Assistance with running a few lines of code at an EXACT time nethatar 5 5,020 Feb-24-2021, 10:43 PM
Last Post: nilamo
  Sorting Elements via parameters pointing to those elements. rpalmer 3 3,624 Feb-10-2021, 04:53 PM
Last Post: rpalmer
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 4,432 Dec-02-2020, 07:50 AM
Last Post: Gilush
  Split dict of lists into smaller dicts of lists. pcs3rd 3 3,458 Sep-19-2020, 09:12 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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