1
l = ["a","1","b","2","c","3",]

I'm trying to print the items in the list above simultaneously. with different text added before every nth element. that is, "alphabet" before 0,2,4 elements and "number" before 1,3,5 elements.

expected output is :

  • alphabet a
  • number 1
  • alphabet b
  • number 2
  • alphabet c
  • number 3'''

what I tried is :

a lot of google searching and applying solutions.learning about print(*l, sep="\n") and how that doesn't help with formatting the output before printing. I tried enumerate but that's really tough to grasp, I tried in "middle_index" code and split the list into two by "step::2" and then print twice etc. it is not working..please let me know how this can be done. thank you.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Den77
  • 19
  • 7
  • 1
    What do you mean similtaneously? what about `print(*list, sep="\n")` but also you really shouldnt use variable names that conflict with python built in names, never call your list as list. as thats a built in keyword – Chris Doyle Apr 08 '20 at 10:28
  • 1
    `print(*list,sep='\n')`? – Ch3steR Apr 08 '20 at 10:29
  • i want to add a string for 0th,2nd and 4rd element and another string for 1st, 3rd and 5h element. print(*list) and print(l,sep= "\n") works, but doesn't work with formatting before printing ( adding text for nth elements ).. – Den77 Apr 08 '20 at 10:32
  • @PirateHat Please update your question with an example so that we can help you – Riccardo Bucco Apr 08 '20 at 10:33
  • sry, i will keep it in mind to not use variable names that way. print(*list,sep='\n') this works but not completely. ' alphabet a number 1 alphabet b – Den77 Apr 08 '20 at 10:36
  • @PirateHat What do you mean it does not work completely? Please edit your question with an example (input and expected output) – Riccardo Bucco Apr 08 '20 at 10:37
  • Wrong data structure. A list is supposed to be an homogenous collection (the only difference between two items should be it's position, not it's type or meaning). You should have a list of `(letter, position)` tuples instead. – bruno desthuilliers Apr 08 '20 at 11:19
  • @bruno, is the slicing and finding index etc are all same for tuples as they are for a list? – Den77 Apr 09 '20 at 22:13

2 Answers2

3

Here's how you can do it:

lst = ["a","1","b","2","c","3"]
s1 = "alphabet"
s2 = "number"
sep = " " # You can change it to "\n"

print(*(f"{s1 if i % 2 == 0 else s2} {x}" for i, x in enumerate(lst)), sep=sep)

Output:

alphabet a number 1 alphabet b number 2 alphabet c number 3

You just need to change the separator accordingly to your needs.

Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50
2

You need to test the index of each element and add the correct additional output before the printed value:

l = ["a","1","b","2","c","3",]

for idx,e in enumerate(l):
    print("alphabet" if not idx%2 else "number", e)

to get

alphabet a
number 1
alphabet b
number 2
alphabet c
number 3

%2 means the idx%2 either returns 0 (for index 0,2,4,6,...) or 1 (for index 1,3,5,...) - 0 is falsy and used in a ternary inside the print statement to print the correct thing in front of the element you take from l.

See

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69