X
X
XWR2021-07-28 17:57:04
Python
XWR, 2021-07-28 17:57:04

How to output non-repeating elements of a python list?

I want to display random 3 elements from the list so that they do not repeat.

import random
a = ["1", "2", "3", "4"]

random.shuffle(a)
print(a[:3])

The code works, but it outputs this: ['2', '3', '1']

And I need it without: ['', '', '']

How can I do that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2021-07-28
@XWR

The code works, but it outputs this: ['2', '3', '1']
And I need it without: ['', '', '']

int(''.join(['2', '3', '1']))   # ['2', '3', '1'] можно заменить на a[:3] 
# 231

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question