0

I want to create possible list of numbers from given list. For ex.

a = [5, 9]

should result in 59, 95.

How do I do it in python. Please note contents of list a could be variable.

Thanks in advance

2
  • 2
    It's not clear what your requirements are. Some more examples, or a fuller description of what you mean by "possible list of numbers" would help. E.g., what's the expected output if a = [5, 5]? What if a = [1, 2, 3]? What if a = [10, 11, 12]? Commented Sep 2, 2018 at 18:44
  • And what if [0, 1]? Commented Sep 3, 2018 at 3:22

1 Answer 1

1

This might be helpful.

import itertools

a = [5,9]
a = "".join(str(i) for i in a)


x = list(itertools.permutations(a))
x = ["".join(tupl) for tupl in x]
print(x)

['59', '95']
Sign up to request clarification or add additional context in comments.

4 Comments

Minor correction x = ["".join(tupl) for tupl in x] should be x = [int("".join(tupl)) for tupl in x] since he is asking for numbers
@khalil : Just perfect ! One more query - which exact study material of python would help me to learn/understand this ?
@VijayGharge I found this site quite usefull python-course.eu/python3_course.php. However, the best way to learn a programming language is get yourself involved in a real project in which you implement the language principles. Python is one of most popular programming language since it can be used in many different disciplines. Myself, I am a scientist who does not bother much about web development frameworks as much as scientific packages like numpy, scipy, etc, available in Python. So, learn what is more relevant to your field of study or job requirement. Good luck.
Thanks alot for advise

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.