1

I am having a list as:

list_1 = ['id', 'name', 'organization', 'notes', 'key_length', 
         'digest', 'validity_start',  'validity_end', 'country_code', 
         'state', 'city', 'organization_name',  'organizational_unit_name', 
         'email', 'common_name', 'extensions', 'serial_number', 'passphrase',
         'created', 'modified']

Now, I am trying to code a list comprehension to get the below list as a final result:

list_2 = ['id', 'name', 'organization', 'notes', 
          'key_length', 'digest', 'validity_start', 'validity_end', 
          'country_code', 'state', 'city', 'organization_name', 'organizational_unit_name',
          'email', 'common_name', 'serial_number', 'certificate', 
          'private_key', 'created', 'modified']

Here's what I am doing:

list_2 = [
            field
            for field in list_1
            if field not in {'extensions', 'passphrase'}
        ]

With the above LOC I am able to get the the desired list except the two fields certificate, & private_key. For that presently I am using the insert method, but I want this to be done in the the list comprehension itself.

AcK
  • 2,063
  • 2
  • 20
  • 27
Manish Shah
  • 331
  • 5
  • 18
  • 1
    I might be wrong, but I didn't find either **certificate** or **private_key** in `list_1`, and thus there is not way of putting them into `list_2` using List Comprehension. List Comprehension is used to substitute [`map`](https://stackoverflow.com/a/67303519/14403987) and [`filter`](https://stackoverflow.com/a/67303519/14403987) functions, but not to add something into a `list`. – Felipe Whitaker Apr 28 '21 at 15:52
  • @Felipe Whitaker, that is True, but we can concatenate two lists before handling the comprehensions. Does [this](https://stackoverflow.com/questions/1720421/how-do-i-concatenate-two-lists-in-python) post answers your question? It would be `for field in list_1 + ['certificate', 'private_key']`. – Thymen Apr 28 '21 at 15:52
  • 1
    @Thymen sure, it is possible to do `list_2 = [...] + ['certificate', 'private_key']`, but then it is not done *inside* the list comprehension. To do it inside, he can follow [DeMO](https://stackoverflow.com/a/67303710/14403987)'s solution, which solves it by concatenating the two lists inside the list comprehension, like `list_2 = [... + []]`. – Felipe Whitaker Apr 28 '21 at 16:00
  • I guess the wording _inside_ the comprehension is a bit vague. For me using `[x for x in list_1 + ['c', 'p']]` would qualify as "inside" the comprehension, albeit not _inside_. Any way the answers from @DeMO perfectly catches it. – Thymen Apr 28 '21 at 17:07

3 Answers3

1

If you wanna replace an item use a mapping dictionary.

map_dict = {'extensions': 'certificate', 'passphrase': 'private_key'}
list_2 = 
    map_dict[field] if field in {'extensions', 'passphrase'}
    else field
    for field in list_1
]
Nk03
  • 14,699
  • 2
  • 8
  • 22
1

If the position in the list does not matter:

[field for 
field in list_1+["certificate", "private_key"] 
...

If it does, you can specify where you want as follows

[field for 
field in list_1[0:17]+['certificate','private_key']+list_1[18::]
...

First example joins list_1 with a list containing "certificate" and "private_key", and the second joins a splice of list_1 with the same list and the rest of list_1.

>>> [field for field in 
list_1[0:17]+['certificate','private_key']+list_1[18::] 
if field not in ['extensions','passphrase']]

#output
['id', 'name', 'organization', 'notes', 'key_length', 'digest',  
'validity_start', 'validity_end', 'country_code', 'state', 'city', 
'organization_name', 'organizational_unit_name', 'email',  
'common_name', 'serial_number', 'certificate', 'private_key',   
'created', 'modified']
DeMO
  • 153
  • 1
  • 8
  • Hey I am required to remove `extensions`, and `passphrase` from `list_1`, with this I am not getting the required result. – Manish Shah Apr 28 '21 at 16:18
  • @ManishShah I added a code block that shows the full code and the output received. – DeMO Apr 28 '21 at 16:31
1

As a frame challenge to you wanting to do a list comprehension, I found that, at least on the data you gave as an example, list comprehension is the slowest, LCVcode's suggestion of list_2 = list(set(list_1) - {'extensions', 'passphrase'}) was faster, and

list1a.remove('extensions')
list1a.remove('passphrase')

was fastest. However, this last method will throw an error if the elements you are trying to remove are not present.

To add elements, you can do list_2 = list(set(list_1) - {'extensions', 'passphrase'})+['certificate', 'private_key']

Acccumulation
  • 3,491
  • 1
  • 8
  • 12