# Passing a string containing keyword arguments to a function, combining into list comprehension

**URL:** <https://discuss.python.org/t/passing-a-string-containing-keyword-arguments-to-a-function-combining-into-list-comprehension/109266>\
**Category:** Python Help\
**Created:** [September 28, 2026, 10:12pm UTC](https://discuss.python.org/t/passing-a-string-containing-keyword-arguments-to-a-function-combining-into-list-comprehension/109266 "2026-09-28T22:12:41Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [September 30, 2026, 6:33pm UTC](https://discuss.python.org/t/passing-a-string-containing-keyword-arguments-to-a-function-combining-into-list-comprehension/109266/22 "2026-09-30T18:33:00Z")

</div>

Where has that, very clever, `ast.parse` solution gone which was posted on here last night, seems to have disappeared? @inventshah or @tstefan if I recall correctly, or did I imagine that.

---

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [September 30, 2026, 6:35pm UTC](https://discuss.python.org/t/passing-a-string-containing-keyword-arguments-to-a-function-combining-into-list-comprehension/109266/23 "2026-09-30T18:35:23Z")

</div>

Passing a string of keyword arguments to a function sounded pretty standard to me, feel sure I have done this many times before in other languages; unfortunately didn’t prove to be in Python.

---

<div class="post-metadata">

**Author:** ![jonathandung](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jonathandung/32/35459_2.png) [@jonathandung](https://discuss.python.org/u/jonathandung)\
**Post date:** [September 30, 2026, 10:03pm UTC](https://discuss.python.org/t/passing-a-string-containing-keyword-arguments-to-a-function-combining-into-list-comprehension/109266/28 "2026-09-30T22:03:37Z")

</div>

What are such languages? I feel like preferring config files over this method (which I, for one, have not heard of) is pretty standard and language-agnostic.

---

<div class="post-metadata">

**Author:** ![ndc86430](https://avatars.discourse-cdn.com/v4/letter/n/d6d6ee/32.png) [@ndc86430](https://discuss.python.org/u/ndc86430)\
**Post date:** [October 1, 2026, 5:10am UTC](https://discuss.python.org/t/passing-a-string-containing-keyword-arguments-to-a-function-combining-into-list-comprehension/109266/29 "2026-10-01T05:10:15Z")

</div>

Is there a reason you need to pass a string containing the arguments, instead of just passing separate arguments?

Here, though, it seems like your goal is really “read the database configuration”, rather than “pass a string of keyword arguments”. Use a config file in one of the various common formats, like those already suggested. Why? Parsers already exist, meaning you don’t have to write and maintain that extra code.

---

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [October 1, 2026, 10:56am UTC](https://discuss.python.org/t/passing-a-string-containing-keyword-arguments-to-a-function-combining-into-list-comprehension/109266/30 "2026-10-01T10:56:09Z")

</div>

@jonathandung Well I’m quite certain I have done things like this in various xBase languages of old for a start.

---

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [October 1, 2026, 11:19am UTC](https://discuss.python.org/t/passing-a-string-containing-keyword-arguments-to-a-function-combining-into-list-comprehension/109266/31 "2026-10-01T11:19:02Z")

</div>

@ndc86430 Because in this circumstance the config string is coming from elsewhere and the actual arguments may not always be fixed.

---

<div class="post-metadata">

**Author:** ![davidism](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/davidism/32/3295_2.png) [@davidism](https://discuss.python.org/u/davidism)\
**Post date:** [October 1, 2026, 4:43pm UTC](https://discuss.python.org/t/passing-a-string-containing-keyword-arguments-to-a-function-combining-into-list-comprehension/109266/36 "2026-10-01T16:43:48Z")

</div>

Closed due to continued off topic posting.

---

<div class="post-metadata">

**Author:** ![adrianofdoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/adrianofdoom/32/22114_2.png) [@adrianofdoom](https://discuss.python.org/u/adrianofdoom)\
**Post date:** [October 1, 2026, 10:48pm UTC](https://discuss.python.org/t/passing-a-string-containing-keyword-arguments-to-a-function-combining-into-list-comprehension/109266/37 "2026-10-01T22:48:05Z")

</div>

Use a Dict comprehension instead of a list comprehension to make the dict.

```python

str_in = "arg1 = 'var1', arg2 = 2, arg3 = True, arg4='var4'"

{sp[0]:sp[1] for sp in [pair.split('=') for pair in str_in.split(',')]}

{'arg1 ': " 'var1'", ' arg2 ': ' 2', ' arg3 ': ' True', ' arg4': "'var4'"}

```

---

<div class="post-metadata">

**Author:** ![adrianofdoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/adrianofdoom/32/22114_2.png) [@adrianofdoom](https://discuss.python.org/u/adrianofdoom)\
**Post date:** [October 1, 2026, 11:01pm UTC](https://discuss.python.org/t/passing-a-string-containing-keyword-arguments-to-a-function-combining-into-list-comprehension/109266/38 "2026-10-01T23:01:37Z")

</div>

**This one is better.**  
You can lose the extra quotes and spaces like this

```python
str_in = "arg1 = 'var1', arg2 = 2, arg3 = True, arg4='var4'"

{sp[0]:sp[1] for sp in [ pair.split('=') for pair in str_in.replace(' ',''
).replace("'",'').split(',')]}

{'arg1 ': 'var1', ' arg2 ': '2', ' arg3 ': 'True', ' arg4': 'var4'}

```

[Previous page](https://discuss.python.org/t/passing-a-string-containing-keyword-arguments-to-a-function-combining-into-list-comprehension/109266.md?page=1)
