2
import re
red_dict= {u'Jharkhand': ['jarka09', 'jarka05'],
 u'Karnataka': ['karnataka2013', 'karnatka2008']}
for key, value in red_dict.items():
    num = re.findall(r'\d+',' '.join(value))
    map(lambda x: x if len(x)==4 else '20'+x, num)
    print num

Getting the result

['09', '05']
['2013', '2008']

Why wasn't '20' appended to the two digit list items?

For ordinary use, I can verify that this approach works

l = ['a','b','cd']
map(lambda x:x if len(x)==2 else 'e'+x,l)

gives

['ea', 'eb', 'cd']

2 Answers 2

4

@falsetru's answer might solve your issue, but just a well formed regex could actually do your Job without any post processing with map

Implementation

for key, value in red_dict.items():
    print [re.sub("^[^\d]+20|[^\d]+", "20", v) for v in value]

Demo

['2009', '2005']
['2013', '2008']

Note

For your particular case, you would actually not even need a Regex. You can simply slice out the trailing two characters and append it to '20'

for key, value in red_dict.items():
    print ['20' + v[-2:] for v in value]
Sign up to request clarification or add additional context in comments.

1 Comment

Use \D instead of [^\d]. + Use r'raw string' for regular expression (especially if there's any backslash).
2

map does not replace the given num. You should assign the return value back to num.

>>> a = [1, 2, 3]
>>> map(lambda x: x + 1, a)
[2, 3, 4]
>>> a
[1, 2, 3]
>>> a = map(lambda x: x + 1, a)
>>> a
[2, 3, 4]

num = map(lambda x: x if len(x)==4 else '20'+x, num)
#^^^^

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.