Welcome, guest | Sign In | My Account | Store | Cart

I have got 20-25+ lists in python and I want to check all the items within the list and put single items into a new list.

Python, 15 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
I have got 20-25+ lists in python and I want to check all the items within the list and put single items into a new list.
['a','b', 'c', 'd', 'e']
['a', 'e', 'c', 'x']
['f', 'g', 'k', 'y']
['a', 'z', 't']**strong text**
.
.
.
.
.
.
.
.
#single elements from number of list into one
new = ['a', 'b','c', 'd', 'e', 'f', 'g'........]

 

1 comment

Johannes S 8 years ago  # | flag

You could first convert your lists into a set. In python, a set is a dictionary that contains only keys and no values. Since keys are unique, duplicate entries are eliminated automatically. Then, convert the set back into a list and print. You can achieve this with a nice one-liner:

print list(set().union(a, b, c))

Created by deriensk on Mon, 4 Apr 2016 (MIT)
Python recipes (4591)
deriensk's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks