-1
  1. Here is my code using for loop, I would like to get the result in step 3

d = []
for c in Cs:
        d.append(c)
        print('c:', d)

  1. My output is below but I would not like it in list form and each line only have 1 value
    c: [0.01]
    c: [0.01, 0.1]
    c: [0.01, 0.1, 1.0]
    c: [0.01, 0.1, 1.0, 10.0]
  1. How to get this result like below that has fixed letter of c: following by the number in the Cs list, one in a row
    c: 0.01
    c: 0.1
    c: 1.0
    c: 10.0
popo
  • 33
  • 7
  • 1
    Does this answer your question? [for loop in Python](https://stackoverflow.com/questions/4170656/for-loop-in-python) – Amit Gupta Jun 28 '21 at 06:44
  • 1
    You say that you want to **print** `c` values in a **for loop** and you show us a list `d`, is this list relevant to the problem? If it is, please include it in the description! – Wolf Jun 28 '21 at 06:44
  • 1
    If you don't want to print the list... then don't print the list? It is confusing to understand what you are asking. Just print `c` instead of `d`? – juanpa.arrivillaga Jun 28 '21 at 06:46
  • **Please** at least tell us what `Cs` is: just another list? Clarifying a question is for what the [edit](https://stackoverflow.com/posts/68158582/edit) function exists. – Wolf Jun 28 '21 at 07:17

3 Answers3

1

You don't need to append it in a list. Just the following code will work.

for c in Cs:
        print('c:', c)
Shreyansh
  • 458
  • 4
  • 11
0

If you want to keep the list, but only want the last element to be displayed then change the line

 print('c:', d)

to

 print('c:', d[-1])
zwitsch
  • 359
  • 3
  • 9
0

Assuming the orders of elements in lists Cs and Ds correspond (c = [name1, name2, name3] and d = [valueOfName1,valueOfName2, valueOfName3]) and you want to output them as pairs like c[0]: d[0]

Ds = [f'value{i}' for i in range(10)]
Cs = [f'name{i}' for i in range(10)]

for c, d in zip(Cs, Ds):
    print(f"{c}: {d}")

output:

name0: value0
name1: value1
name2: value2
name3: value3
name4: value4
name5: value5
name6: value6
name7: value7
name8: value8
name9: value9

or, to have the fixed c in output:

Ds = [f'value{i}' for i in range(10)]

for d in Ds:
    print(f"c: {d}")

output:

c: value0
c: value1
c: value2
c: value3
c: value4
c: value5
c: value6
c: value7
c: value8
c: value9
  • @Wolf I wasnt sure if he wanted to output a key-value pair like `name1: value1` then I read he wanted the fixed `c: ` output so I edited and added another way. Whats so confusing?? – lizardowl5151 Jun 28 '21 at 06:55
  • @Wolf It's a rhetorical question. I wasnt expectiong a direct answer. The code i provided is the _answer_. – lizardowl5151 Jun 28 '21 at 07:01
  • You know that you can [edit](https://stackoverflow.com/posts/68158725/edit) your question? Which would be the best option to answer *my* rhetorical question: Why should it make sense to tell an absolute beginner anything about `zip` before answering their actual question? – Wolf Jun 28 '21 at 07:08
  • I did answer their actual question. **If** he is an _absolute_ _beginner_ and does not recognize `zip`, it would be good for him to google `zip()` and learn about it. If he has a question or `zip` in my code does not make sense to him, he can ask about it in the comments. What is the issue???? – lizardowl5151 Jun 28 '21 at 07:16
  • Your comment is very discouraging. And your answer still shows the issue. Perhaps you could see it more easily if it were the answer to a test on a (failed) job application? Already the [tour](https://stackoverflow.com/tour) mentions the value of distraction. – Wolf Jun 28 '21 at 07:36
  • Youre right, edited for readability and sake of completeness :) – lizardowl5151 Jun 28 '21 at 07:46
  • For the sake of completeness , where do you see a `Ds` in the question? – Wolf Jun 28 '21 at 07:55
  • I see `d`. Like i said i thought maybe he wanted `value_of_c : value_of_d`. – lizardowl5151 Jun 28 '21 at 07:58