Prajakta Dumbre
Prajakta Dumbre

Reputation: 251

How to create simple list from array of list using Python?

Here is the list:

But I want to remove [] from list. Output should be:

Upvotes: 0

Views: 53

Answers (4)

napuzba
napuzba

Reputation: 6288

You can use itertools.chain :

import itertools
def unlist_items(list1):
    return list(itertools.chain(*list1))

list1 = [[('mobile','VB')],[('margin','NN')],[('and','CC')] ]
print( unlist_items( list1 ) ) # prints [('mobile', 'VB'), ('margin', 'NN'), ('and', 'CC')]

Upvotes: 0

Saeid
Saeid

Reputation: 4255

Using list comprehension

L = [[('mobile','VB')],[('margin','NN')],[('and','CC')]]
R = [ x[0] for x in L ]

Upvotes: 1

Abbbas khan
Abbbas khan

Reputation: 302

d={'a':'apple','b':'ball'}
d.keys()   # will give you all keys in list
['a','b']
d.values()  # will give you values in list
['apple','ball']
d.items()  # will give you pair tuple of key and value
[('a','apple'),('b','ball')
print keys,values method one

for x in d.keys():
    print x +" => " + d[x]  

'

var arr= []; // create an empty array

    arr.push({
        key:   "Name",
        value: "value"
    });
 OR

    var array = {};

    array ['key1'] = "testing1";
    array ['key2'] = "testing2";
    array ['key3'] = "testing3";
    console.log(array);

or like that
    var input = [{key:"key1", value:"value1"},{key:"key2", value:"value2"}];

Upvotes: 0

Kenjin
Kenjin

Reputation: 136

One solution is to iterate over the list and take the first item inside and add it to a new list.

In [1] _list = [[('mobile','VB')],[('margin','NN')],[('and','CC')]]
In [2] cleaned = []
In [3] for item in _list: cleaned.append(item[0])

In [4] print(cleaned)
Out[4] [('mobile', 'VB'), ('margin', 'NN'), ('and', 'CC')]

Upvotes: 0

Related Questions