anonymou-dipsite
anonymou-dipsite

Reputation: 1

How to remove parenthesis, commas, and quotation marks from list items when two lists are combined together

I have been trying to create a simple word list with 260 combinations. I created two lists and combined them to get all combinations

letter = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
number = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

c = [(x,y) for x in letter for y in number]
print(c)

The result is

[('a', 0), ('a', 1), ('a', 2), ('a', 3), ('a', 4), ('a', 5), ('a', 6), ('a', 7), ('a', 8), ('a', 9), ('b', 0), ('b', 1), ('b', 2), ('b', 3), ('b', 4), ('b', 5), ('b', 6), ('b', 7), ('b', 8), ('b', 9), ('c', 0), ('c', 1), ('c', 2), ('c', 3), ('c', 4), ('c', 5), ('c', 6), ('c', 7), ('c', 8), ('c', 9), ('d', 0), ('d', 1), ('d', 2), ('d', 3), ('d', 4), ('d', 5), ('d', 6), ('d', 7), ('d', 8), ('d', 9), ('e', 0), ('e', 1), ('e', 2), ('e', 3), ('e', 4), ('e', 5), ('e', 6), ('e', 7), ('e', 8), ('e', 9), ('f', 0), ('f', 1), ('f', 2), ('f', 3), ('f', 4), ('f', 5), ('f', 6), ('f', 7), ('f', 8), ('f', 9), ('g', 0), ('g', 1), ('g', 2), ('g', 3), ('g', 4), ('g', 5), ('g', 6), ('g', 7), ('g', 8), ('g', 9), ('h', 0), ('h', 1), ('h', 2), ('h', 3), ('h', 4), ('h', 5), ('h', 6), ('h', 7), ('h', 8), ('h', 9), ('i', 0), ('i', 1), ('i', 2), ('i', 3), ('i', 4), ('i', 5), ('i', 6), ('i', 7), ('i', 8), ('i', 9), ('j', 0), ('j', 1), ('j', 2), ('j', 3), ('j', 4), ('j', 5), ('j', 6), ('j', 7), ('j', 8), ('j', 9), ('k', 0), ('k', 1), ('k', 2), ('k', 3), ('k', 4), ('k', 5), ('k', 6), ('k', 7), ('k', 8), ('k', 9), ('l', 0), ('l', 1), ('l', 2), ('l', 3), ('l', 4), ('l', 5), ('l', 6), ('l', 7), ('l', 8), ('l', 9), ('m', 0), ('m', 1), ('m', 2), ('m', 3), ('m', 4), ('m', 5), ('m', 6), ('m', 7), ('m', 8), ('m', 9), ('n', 0), ('n', 1), ('n', 2), ('n', 3), ('n', 4), ('n', 5), ('n', 6), ('n', 7), ('n', 8), ('n', 9), ('o', 0), ('o', 1), ('o', 2), ('o', 3), ('o', 4), ('o', 5), ('o', 6), ('o', 7), ('o', 8), ('o', 9), ('p', 0), ('p', 1), ('p', 2), ('p', 3), ('p', 4), ('p', 5), ('p', 6), ('p', 7), ('p', 8), ('p', 9), ('q', 0), ('q', 1), ('q', 2), ('q', 3), ('q', 4), ('q', 5), ('q', 6), ('q', 7), ('q', 8), ('q', 9), ('r', 0), ('r', 1), ('r', 2), ('r', 3), ('r', 4), ('r', 5), ('r', 6), ('r', 7), ('r', 8), ('r', 9), ('s', 0), ('s', 1), ('s', 2), ('s', 3), ('s', 4), ('s', 5), ('s', 6), ('s', 7), ('s', 8), ('s', 9), ('t', 0), ('t', 1), ('t', 2), ('t', 3), ('t', 4), ('t', 5), ('t', 6), ('t', 7), ('t', 8), ('t', 9), ('u', 0), ('u', 1), ('u', 2), ('u', 3), ('u', 4), ('u', 5), ('u', 6), ('u', 7), ('u', 8), ('u', 9), ('v', 0), ('v', 1), ('v', 2), ('v', 3), ('v', 4), ('v', 5), ('v', 6), ('v', 7), ('v', 8), ('v', 9), ('w', 0), ('w', 1), ('w', 2), ('w', 3), ('w', 4), ('w', 5), ('w', 6), ('w', 7), ('w', 8), ('w', 9), ('x', 0), ('x', 1), ('x', 2), ('x', 3), ('x', 4), ('x', 5), ('x', 6), ('x', 7), ('x', 8), ('x', 9), ('y', 0), ('y', 1), ('y', 2), ('y', 3), ('y', 4), ('y', 5), ('y', 6), ('y', 7), ('y', 8), ('y', 9), ('z', 0), ('z', 1), ('z', 2), ('z', 3), ('z', 4), ('z', 5), ('z', 6), ('z', 7), ('z', 8), ('z', 9)]

Now I want to print these items without any spaces and in different lines so that it can be used as a word list; for example the required result would look like:

a0
a1
a2
a3
a4
.
.
.
.
z9

Upvotes: 0

Views: 372

Answers (3)

E. Ducateme
E. Ducateme

Reputation: 4248

The easiest way to do this is probably a for loop. The "tricky" part is converting the values so that they can be joined together (concatenated).

There are several techniques available to you:

  • convert the values up front
  • convert the values when you use them

From your code:

letter = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
          "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", 
          "u", "v", "w", "x", "y", "z"]
number = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Conversion up front

Since each of the letters are already strings, you don't need to convert them to the str datatype, but you do need to convert the numbers (which are of the int datatype) into the the str datatype.

As you parse the y values in the number list, you can convert them to str.

c = [(x, str(y)) for x in letter for y in number]

for pair in c:
    print(pair[0] + pair[1])

Conversion when you use them

Conversely, if you want to hold off on converting the int to str until the last minute, you can do the conversion when you print:

c = [(x, y) for x in letter for y in number]

for pair in c:
    print(pair[0] + str(pair[1]))

Tuple unpacking technique:

If you would rather keep the print statement simple and clean and use variable names that make more sense, this is an option, as well: tuple unpacking in the for loop target variable. Meaning we replace the target variable (pair) with two target variables (one to accept each value in the tuple >>> ltr and num):

c = [(x, str(y)) for x in letter for y in number]

for ltr, num in c:
    print(ltr + num)

Upvotes: 0

Elmex80s
Elmex80s

Reputation: 3504

Pro answer:

print('\n'.join(x + str(y) for x, y in c))

Upvotes: 2

narendra-choudhary
narendra-choudhary

Reputation: 4826

Use a loop:

c = [(x,y) for x in letter for y in number]
for _c in c:
    print(str(_c[0])+ str(_c[1]))

Upvotes: 0

Related Questions