user3640472
user3640472

Reputation: 115

to merge lists in python

I have the following python code:

import subprocess

def disk():
for i in ('/tmp' , '/usr/mware' , '/var' , '/var/mware'):
    df1 = subprocess.Popen(['df','-h', i], stdout=subprocess.PIPE).communicate()[0].split()
    df1.remove("on")
    print df1

disk()

I am getting the following output:

['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted', '/dev/mapper/rootvg-tmplv', '2.0G', '39M', '2.0G', '2%', '/tmp']
['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted', '/dev/mapper/appvg-usrmwarelv', '20G', '33M', '20G', '1%', '/usr/mware']
['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted', '/dev/mapper/rootvg-varlv', '10G', '3.8G', '6.3G', '38%', '/var']
['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted', '/dev/mapper/appvg-varmwarelv', '20G', '33M', '20G', '1%', '/var/mware']

I want to merge these lists and remove duplicates.

Upvotes: 0

Views: 135

Answers (6)

Akshay Kandul
Akshay Kandul

Reputation: 612

import subprocess

def disk():
    df_set = set()
    for i in ('/tmp', '/usr/mware', '/var', '/var/mware'):
        df1 = subprocess.Popen(['df','-h', i], stdout=subprocess.PIPE).communicate()[0].split()
        df1.remove("on")
        df_set = df_set.union(set(df1))
    print(list(df_set))
disk()

Above code will merge your list and remove duplicates from it.

Upvotes: 1

MSeifert
MSeifert

Reputation: 152795

You could use the set.union method:

list(set(df1).union(df2, df3, df4))  # assuming the invidual lists are called df1, df2, ...

a set is an unordered collection of unique objects. You'll lose the duplicates but also lose your order. In case you want it ordered and without duplicates you could use OrderedDict:

from collections import OrderedDict
from itertools import chain

list(OrderedDict.fromkeys(chain(a, b, c, d)))

Upvotes: 0

Sachin
Sachin

Reputation: 3674

import subprocess

# prepare a list of columns in the beginning
df_out = [['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted']]

def disk():
    for i in ('/tmp' , '/usr/mware' , '/var' , '/var/mware'):
        df1 = subprocess.Popen(['df','-h', i], 
                    stdout=subprocess.PIPE).communicate()[0].split()
        df1.remove("on")

        # append the remaining columns in the list 'df1'
        df_out.append(df1[6:])

disk()

# print the final output
print df_out

This will give you the output as -

[['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted'],
 ['/dev/mapper/rootvg-tmplv', '2.0G', '39M', '2.0G', '2%', '/tmp'],
 ['/dev/mapper/appvg-usrmwarelv', '20G', '33M', '20G', '1%', '/usr/mware'],
 ['/dev/mapper/rootvg-varlv', '10G', '3.8G', '6.3G', '38%', '/var'],
 ['/dev/mapper/appvg-varmwarelv', '20G', '33M', '20G', '1%', '/var/mware']]

Upvotes: 0

frhyme
frhyme

Reputation: 1036

merging lists is so simple.

a= ['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted', '/dev/mapper/rootvg-tmplv', '2.0G', '39M', '2.0G', '2%', '/tmp']
b= ['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted', '/dev/mapper/appvg-usrmwarelv', '20G', '33M', '20G', '1%', '/usr/mware']
c= ['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted', '/dev/mapper/rootvg-varlv', '10G', '3.8G', '6.3G', '38%', '/var']
d = ['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted', '/dev/mapper/appvg-varmwarelv', '20G', '33M', '20G', '1%', '/var/mware']
remove_duplicates = list(set(a+b+c+d))

Upvotes: 1

Błotosmętek
Błotosmętek

Reputation: 12927

Why do you spawn separate processes for every filesystem to check, while df is able to check all of them in one run?

df1 = subprocess.Popen(['df','-h', '/tmp' , '/usr/mware' , '/var' , '/var/mware'], stdout=subprocess.PIPE).communicate()[0].split()

Upvotes: -1

Tbaki
Tbaki

Reputation: 1003

You can concatenate and use set :

def disk():
    df = []
    for i in ('/tmp' , '/usr/mware' , '/var' , '/var/mware'):
        df1 = subprocess.Popen(['df','-h', i], stdout=subprocess.PIPE).communicate()[0].split()
        df1.remove("on")
        df = df + df1
    return set(df)

Upvotes: 0

Related Questions