0

I have a list in python that I am printing to the screen. The current format is

Option Key: [0, 3, 6, 8, 8, 5, 2, 0, 7, 1, 9, 5, 0, 7, 7, 4, 2]

I would like it to be Option Key: 0 3688 5207 1950 7742

I think I need to do something with the .format method but I"m not sure.

The code looks like this

    option_key.append(int(m_key_code))

    print("Option Key: ", option_key)
3
Contributors
3
Replies
44
Views
2 Years
Discussion Span
Last Post by vegaseat
1

Without thinking ...

mylist = [0, 3, 6, 8, 8, 5, 2, 0, 7, 1, 9, 5, 0, 7, 7, 4, 2]

s = ""
n = 1
for ix, k in enumerate(mylist):
    if ix == n:
        s += ' '
        n += 4
    s += str(k)

print(s)  # 0 3688 5207 1950 7742
2

Another concept of Python is slicing ...

mylist = [0, 3, 6, 8, 8, 5, 2, 0, 7, 1, 9, 5, 0, 7, 7, 4, 2]

s = "".join(str(n) for n in mylist)

s2 = "0"
for n in range(0, 17, 4):
    s2 += " " + s[n+1:n+5]

print(s2)    # 0 3688 5207 1950 7742
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.